我从 .csv
文件中读取了一些天气数据作为名为“天气”的数据框。问题是其中一列的数据类型是 object
。这很奇怪,因为它指示温度。如何将其更改为 float
数据类型?我试过 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
原文由 Almog Woldenberg 发布,翻译遵循 CC BY-SA 4.0 许可协议
pandas.Series.astype
您还可以使用
pd.to_numeric
将列从对象转换为浮点有关如何使用它的详细信息,请查看此链接:http: //pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html
例子 :
convert_objects
NaN
… 所以在使用它时要小心。