[英]Pandas / How to convert scientific notation stored as strings into float?
I have a list with values stored as strings.我有一个列表,其中的值存储为字符串。 Some of the values are stored with scientific notation and others are floats.
一些值以科学记数法存储,而其他值则是浮点数。 I tried to cast them using .astype(float) but It didn't work.
我试图使用 .astype(float) 来投射它们,但它没有用。 Any idea on how to deal with this?
关于如何处理这个问题的任何想法?
My goal is to get the column as float.我的目标是让列成为浮动。
df['Colum1] = ['5,4e-05','3,35e-05','0,0001125','0,0001335']
First you have to replace the commas for dots, then you can just cas to float
:首先,您必须将逗号替换为点,然后您可以使用 cas 来
float
:
df.Colum1.str.replace(',', '.').astype(float)
Also to prevent from any errors, pd.to_numeric
is probably the best option:同样为了防止任何错误,
pd.to_numeric
可能是最好的选择:
pd.to_numeric(df.Col1.str.replace(',', '.'), errors='coerce')
Here's an example:下面是一个例子:
df = pd.DataFrame({'Col1':['5,4e-05','3,35e-05','0,0001125','0,0001335']})
pd.to_numeric(df.Col1.str.replace(',', '.'), errors='coerce')
0 0.000054
1 0.000034
2 0.000112
3 0.000133
Name: Col1, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.