python pandas DataFrame 清除非数字类型、重新索引

时间:2021-08-29 19:17:07   收藏:0   阅读:11

将DataFrame格式的数据中不是数字类型的去掉并重新索引:

首先将所有的值都转成整数字类型

df[‘io‘]=pd.to_numeric(df[‘io‘],‘coerce‘)
df[‘res‘]=pd.to_numeric(df[‘res‘],‘coerce‘)

coerce是将不能转为数字类型的都变成NaN,其他两个是ignore:忽略,也就是不转换,raise:报错 默认是raise 

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_numeric.html

然后将所有NaN都删除

df=df.dropna()

后面也有参数,可以选择按行删除还是列删除;有一个NaN就删除还是全都是NaN才删除

https://jingyan.baidu.com/article/ff4116250521ce12e4823795.html

重新索引:

df=df.reset_index(drop=True)

注意这里不能用reindex,而且后面要加上 drop=Ture,不然原来的index还是会保留着

 

原文:https://www.cnblogs.com/perryhan/p/15194815.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!