无法将 Pandas 列从对象转换为 Python 中的浮动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37141685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unable to convert pandas columns from object to float in python
提问by Houda L
I'm making using a panda frame containing columns like below:
我正在使用包含如下列的Pandas框架:
data = {'chindice': [ '-1', '5.89 e-06', '6.76 e-06', '6.31 e-06', '1',
'4', np.nan],
'target': ['classe1', 'classe2', 'classe3', np.nan,'classe5', 'classe4', 'classe5' ],
}
df = pd.DataFrame(data)
I need to use the columns "chindice" as float, but I couldnt because the columns dtype is 'object'. Any help would be appreciated. I am a newbie to pandas. Thanks
我需要使用列“chindice”作为浮动,但我不能,因为列 dtype 是“对象”。任何帮助,将不胜感激。我是Pandas的新手。谢谢
回答by EdChum
You can use to_numeric
after stripping the problematic space in your scientific notation entries using str.replace
:
您可以在使用以下方法to_numeric
去除科学记数法条目中的问题空间后使用str.replace
:
In [15]:
df['chindice'] = pd.to_numeric(df['chindice'].str.replace(' ',''), errors='force')
df
Out[15]:
chindice target
0 -1.000000 classe1
1 0.000006 classe2
2 0.000007 classe3
3 0.000006 NaN
4 1.000000 classe5
5 4.000000 classe4
6 NaN classe5
Don't worry about the display, the real value is still there:
不用担心显示,真正的价值还在:
In [17]:
df['chindice'].iloc[1]
Out[17]:
5.8900000000000004e-06
回答by jezrael
You can use replace
arbitrary whitespace \s+
and then cast by astype
to float
:
您可以使用replace
任意空格\s+
,然后通过astype
to强制转换float
:
df['chindice'] = df.chindice.str.replace(r'\s+','').astype(float)
print df
chindice target
0 -1.000000 classe1
1 0.000006 classe2
2 0.000007 classe3
3 0.000006 NaN
4 1.000000 classe5
5 4.000000 classe4
6 NaN classe5
#temporaly display with precision 8
with pd.option_context('display.precision', 8):
print df
chindice target
0 -1.00000000 classe1
1 0.00000589 classe2
2 0.00000676 classe3
3 0.00000631 NaN
4 1.00000000 classe5
5 4.00000000 classe4
6 NaN classe5