如何在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

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...