微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Pandas Python中将单元格中的数字更改为单词“ Bus”

我有一个在同一列中同时包含数字和文本的数据框,所有这些都是对象类型.当文本保留为对象时,如何仅将单元格中的数字转换为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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐