pandas 在python中将字符串转换为浮点数的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46322258/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:29:36  来源:igfitidea点击:

Trouble converting string to float in python

pythonpandas

提问by Minsky

I am fairly new to Python so forgive me this simple question. I'm trying to convert string to float. Here is a sample of the data:

我对 Python 相当陌生,所以请原谅我这个简单的问题。我正在尝试将字符串转换为浮点数。以下是数据示例:

0     10.65%
1      7.90%

When I try:

当我尝试:

 df['int_rate'] = df['int_rate'].astype('float')

I get:

我得到:

ValueError: could not convert string to float: '13.75%'

When I try:

当我尝试:

df['int_rate'] = df['int_rate'].replace("%","", inplace=True) 

And check my data, I get:

检查我的数据,我得到:

0     None
1     None

Any ideas what I'm doing wrong? Many thanks!

任何想法我做错了什么?非常感谢!

采纳答案by jezrael

You can use Series.replacewith parameter regex=Truefor replace substrings:

您可以使用Series.replacewith 参数regex=True来替换substrings:

df = pd.DataFrame({'int_rate':['10.65%','7.90%']})
df['int_rate'] = df['int_rate'].replace("%","", regex=True).astype(float)
print (df)
   int_rate
0     10.65
1      7.90

Or Series.str.replace:

Series.str.replace

df['int_rate'] = df['int_rate'].str.replace("%","")
print (df)
  int_rate
0    10.65
1     7.90
2         

Or Series.str.rstrip:

Series.str.rstrip

df['int_rate'] = df['int_rate'].str.rstrip("%").astype(float)
print (df)
   int_rate
0     10.65
1      7.90

See difference without it:

看到没有它的区别:

df = pd.DataFrame({'int_rate':['10.65%','7.90%', '%']})

df['int_rate_subs'] = df['int_rate'].replace("%","", regex=True)
df['int_rate_val'] = df['int_rate'].replace("%","")
print (df)
  int_rate int_rate_subs int_rate_val
0   10.65%         10.65       10.65%
1    7.90%          7.90        7.90%
2        %                           

回答by Guillaume

As you guessed, ValueError: could not convert string to float: '13.75%'indicates that the %character blocks the convertion.

如您所料,ValueError: could not convert string to float: '13.75%'表示该%字符阻止了转换。

Now when you try to remove it:

现在,当您尝试删除它时:

df['int_rate'] = df['int_rate'].replace("%","", inplace=True) 

You set inplace=Truein your replacement, which as the name suggests changes the dataframe in-place, so replace()method call returns None. Thus you store Nonein df['int_rate']and end up with a column containing only Nonevalues. You should either do:

您设置inplace=True了替换,顾名思义,它会就地更改数据帧,因此replace()方法调用返回None. 因此,你存储Nonedf['int_rate']与仅包含一列最终None值。你应该这样做:

df['int_rate'] = df['int_rate'].replace("%","") 

or

或者

df['int_rate'].replace("%","", inplace=True)

回答by Tyler

Since you're using a string, you could convert the value to a float using

由于您使用的是字符串,因此可以使用将值转换为浮点数

float(df['int_rate'][:-1])

This reads the string from the first position to the second to last position, 10.65 instead of 10.65%.

这将读取从第一个位置到第二个到最后一个位置的字符串,10.65 而不是 10.65%。