我有一个在同一列中同时包含数字和文本的数据框,所有这些都是对象类型.当文本保留为对象时,如何仅将单元格中的数字转换为int?
我尝试使用熊猫功能>> pd.to_numeric(df,errors =’ignore’)
但是只有没有文本的列才转换为浮点数.其余作为对象
27 72 27 72 None None None None
34 34 None None None None None None
MRT MRT None None None None None None
MRT MRT None None None None None None
MRT MRT None None None None None None
121 195 121 195 None None None None
175 147 147 175 None None None None
33 33 None None None None None None
Bus Bus Bus Bus None None None None
Bus Bus None None None None None None
MRT MRT None None None None None None
MRT MRT None None None None None None
MRT MRT None None None None None None
Bus Bus Bus Bus None None None None
Bus Bus Bus Bus None None None None
Bus Bus None None None None None None
解决方法:
IIUC使用带有掩码的to_numeric
yourdf=df.mask(df.apply(pd.to_numeric,errors='coerce',axis=1).notnull(),'BUS')
yourdf
Out[631]:
27 72 27.1 72.1 None None.1 None.2 None.3
0 BUS BUS None None None None None None
1 MRT MRT None None None None None None
2 MRT MRT None None None None None None
3 MRT MRT None None None None None None
4 BUS BUS BUS BUS None None None None
5 BUS BUS BUS BUS None None None None
6 BUS BUS None None None None None None