我有一个数据框,该数据框在同一列中同时包含数字和文本,所有这些都是对象类型。在文本保留为对象的情况下,如何仅将单元格中的数字转换为int
?
我尝试使用pandas function >> 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
答案 0 :(得分:2)
IIUC使用def composed2(a: Int, b: Int, c: Int) =
composed1(a, b) andThen lastOne(c)
composed2(2, 2, 4)(5) //res0: Int = 13
和to_numeric
mask
答案 1 :(得分:2)
如果您的真实数据看起来像这样,即不包含诸如12.3
之类的字符串,则可以尝试将其转换为to_numeric
并用'Bus'填充non-na
df[df.apply(pd.to_numeric,
args={'errors':'ignore'})
.notnull()] = 'Bus'
测试数据:
df = pd.DataFrame({'a':[12, 'None', None],
'b':[23, 'MRT', None]})
给予:
a b
0 Bus Bus
1 None MRT
2 None None
答案 2 :(得分:0)
在数据框中,您可以使用replace()
dataframe.replace(old_value, new_value)
像这样