python_字段类型转换&类型替换(python字典类型转换成字符串)

网友投稿 587 2022-08-24


python_字段类型转换&类型替换(python字典类型转换成字符串)

字段类型转换 替换

#############################################################判断字段类型print('Distance 类型:',dfoff['Distance'].unique())type(dfoff['Distance'].unique()[2])dfoff['Distance'].unique()[2] == np.nantype(np.nan)##将float类型变换为int类型#1.将nulL值填充为0 填充为nulldfoff['Date_received']=dfoff['Date_received'].fillna(0)#加了引号相当于字符,不要加dfoff['distance'] = dfoff['Distance'].fillna(-1)dfoff['Date_received']=dfoff['Date_received'].fillna('null')#2.0 转换类型 替换#把数据中的null值全部替换为-1t4.replace('null',-1,inplace=True)t4.distance = t4.distance.astype('int')#再把数据中的-1全部替换为NaNt4.replace(-1,np.nan,inplace=True)dfoff['distance'] = dfoff['Distance'].replace(np.nan, -1)dfoff['weekday'].replace('null', np.nan)dfoff['Date_received']=dfoff['Date_received'].astype('int')#转换为string类型 dfoff['Date']=dfoff['Date'].astype('str')#查看某字段下所有数据种类print('Discount_rate 类型:\n',dfoff['Discount_rate'].unique())#统计各类型的数量print(dfoff['label'].value_counts())#字符拼接 生成列名weekdaycols = ['weekday_' + str(i) for i in range(1,8)]####################################################################日期字段筛选off_train = pd.read_csv('C:\data\O2O_tianchi\ccf_offline_stage1_train.csv',header=0)off_train.head()off_train.info()#float类型较难筛选, 可以把日期字段转换为int类型, 不过注意,字段有空值转换会报错,先替换空值off_train['date']=off_train['Date'].fillna(0)off_train['date']=off_train['date'].astype('int')feature3 = off_train[((off_train['date'] >= 20160315)&(off_train.date<=20160630))|((off_train['date']==0)&(off_train['Date_received']>=20160315)&(off_train['Date_received']<=20160630))]feature3.head()


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python_日期相关操作(python处理日期数据)
下一篇:SpringBoot+MybatisPlus+Mysql+Sharding
相关文章

 发表评论

暂时没有评论,来抢沙发吧~