任务2 数据清洗
def preprocessingData(data):
# 填充缺失值
data['rentType'][data['rentType'] == '--'] = '未知方式'
# 转换object类型数据
columns = ['rentType','communityName','houseType', 'houseFloor', 'houseToward', 'houseDecoration', 'region', 'plate']
for feature in columns:
data[feature] = LabelEncoder().fit_transform(data[feature])
# 将buildYear列转换为整型数据
buildYearmean = pd.DataFrame(data[data['buildYear'] != '暂无信息']['buildYear'].mode())
data.loc[data[data['buildYear'] == '暂无信息'].index, 'buildYear'] = buildYearmean.iloc[0, 0]
data['buildYear'] = data['buildYear'].astype('int')
# 处理pv和uv的空值
data['pv'].fillna(data['pv'].mean(), inplace=True)
data['uv'].fillna(data['uv'].mean(), inplace=True)
data['pv'] = data['pv'].astype('int')
data['uv'] = data['uv'].astype('int')
# 分割交易时间
def month(x):
month = int(x.split('/')[1])
return month
def day(x):
day = int(x.split('/')[2])
return day
data['month'] = data['tradeTime'].apply(lambda x: month(x))
data['day'] = data['tradeTime'].apply(lambda x: day(x))
# 去掉部分特征
data.drop('city', axis=1, inplace=True)
data.drop('tradeTime', axis=1, inplace=True)
data.drop('ID', axis=1, inplace=True)
return data
data_train = preprocessingData(data_train)
来源:CSDN
作者:yalipf
链接:https://blog.csdn.net/yalipf/article/details/103918553