我想将pandas系列从object转换为float,但保留其他字符串。
以下是代码段: -
In [37]: df = pd.DataFrame(['-1.0', 'hello', '0.5'])
In [38]: df[0]
Out[38]:
0 -1.0
1 hello
2 0.5
Name: 0, dtype: object
我想要的是: -
In [43]: df[0]
Out[43]:
0 -1.0
1 hello
2 0.5
Name: 0, dtype: float64
我尝试使用此功能但将hello
转换为NaN
我不想要的内容: -
In [41]: pd.to_numeric(df[0], errors='coerce')
Out[41]:
0 -1.0
1 NaN
2 0.5
Name: 0, dtype: float64
我尝试了errors='ignore'
,但它没有将对象转换为浮点数: -
In [44]: pd.to_numeric(df[0], errors='ignore')
Out[44]:
0 -1.0
1 hello
2 0.5
Name: 0, dtype: object
有人可以帮我解决这个问题吗?
感谢。
答案 0 :(得分:0)
获得您想要的结果:
In [29]:
df1 = pd.to_numeric(df[0], errors='coerce')
df1
Out[29]:
0 -1.0
1 NaN
2 0.5
Name: 0, dtype: float64
In [32]:
df1 = df1.fillna(df[0])
df1.iloc[0]
Out[32]:
-1.0
In [33]:
df1.iloc[1]
Out[33]:
'hello'
In [34]:
df1.iloc[-1]
Out[34]:
0.5
答案 1 :(得分:0)
有一个肮脏的黑客来实现你想要的东西:
In [73]: new = pd.to_numeric(df[0], errors='coerce')
In [74]: new
Out[74]:
0 -1.0
1 NaN
2 0.5
Name: 0, dtype: float64
In [75]: new2 = new.combine_first(df[0])
In [76]: new2
Out[76]:
0 -1 # element type: float
1 hello # element type: str
2 0.5 # element type: float
Name: 0, dtype: object
您可以将数值应用于仅数字元素
In [78]: new2.iloc[[0,2]] += 1
In [79]: new2
Out[79]:
0 0
1 hello
2 1.5
Name: 0, dtype: object
但要注意:
In [80]: new2 += 10
...
TypeError: must be str, not int
...