Python 将对象转换为浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48094854/
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
Python convert object to float
提问by Almog Woldenberg
I read some weather data from a csv file as a dataframe named "weather". The problem is that one of the columns' data type is an object. this is weird beacuse it indicates temperature... anyway, how to I change it to a float? I tried to_numeric but it can't parse it.
我从一个 csv 文件中读取了一些天气数据作为名为“weather”的数据框。问题是列的数据类型之一是对象。这很奇怪,因为它表示温度……无论如何,我如何将其更改为浮点数?我试过 to_numeric 但它无法解析它。
weather.info()
weather.head()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 304 entries, 2017-01-01 to 2017-10-31
Data columns (total 2 columns):
Temp 304 non-null object
Rain 304 non-null float64
dtypes: float64(1), object(1)
memory usage: 17.1+ KB
Temp Rain
Date
2017-01-01 12.4 0.0
2017-02-01 11 0.6
2017-03-01 10.4 0.6
2017-04-01 10.9 0.2
2017-05-01 13.2 0.0
回答by Jai
- You can use
pandas.Series.astype
You can do something like this :
weather["Temp"] = weather.Temp.astype(float)
You can also use
pd.to_numeric
that will convert the column from object to float- For details on how to use it checkout this link :http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html
Example :
s = pd.Series(['apple', '1.0', '2', -3]) print(pd.to_numeric(s, errors='ignore')) print("=========================") print(pd.to_numeric(s, errors='coerce'))
Output:
0 apple 1 1.0 2 2 3 -3 ========================= dtype: object 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64
In your case you can do something like this:
weather["Temp"] = pd.to_numeric(weather.Temp, errors='coerce')
- Other option is to use
convert_objects
Example is as follows
>> pd.Series([1,2,3,4,'.']).convert_objects(convert_numeric=True) 0 1 1 2 2 3 3 4 4 NaN dtype: float64
You can use this as follows:
weather["Temp"] = weather.Temp.convert_objects(convert_numeric=True)
I have showed you examples because if any of your column won't have a number then it will be converted to
NaN
... so be careful while using it
- 您可以使用
pandas.Series.astype
你可以这样做:
weather["Temp"] = weather.Temp.astype(float)
您还可以使用
pd.to_numeric
它将列从对象转换为浮动- 有关如何使用它的详细信息,请查看此链接:http: //pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html
例子 :
s = pd.Series(['apple', '1.0', '2', -3]) print(pd.to_numeric(s, errors='ignore')) print("=========================") print(pd.to_numeric(s, errors='coerce'))
输出:
0 apple 1 1.0 2 2 3 -3 ========================= dtype: object 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64
在您的情况下,您可以执行以下操作:
weather["Temp"] = pd.to_numeric(weather.Temp, errors='coerce')
- 其他选择是使用
convert_objects
例子如下
>> pd.Series([1,2,3,4,'.']).convert_objects(convert_numeric=True) 0 1 1 2 2 3 3 4 4 NaN dtype: float64
您可以按如下方式使用它:
weather["Temp"] = weather.Temp.convert_objects(convert_numeric=True)
我已经向您展示了示例,因为如果您的任何列没有数字,那么它将被转换为
NaN
......所以在使用它时要小心
回答by Almog Woldenberg
I eventually used:
我最终使用了:
weather["Temp"] = weather["Temp"].convert_objects(convert_numeric=True)
It worked just fine, except that I got the following message.
它工作得很好,除了我收到以下消息。
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: FutureWarning:
convert_objects is deprecated. Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.