Pandas:转换为数字,必要时创建 NaN [英] Pandas: Converting to numeric, creating NaNs when necessary
问题描述
假设我在数据框中有一列有一些数字和一些非数字
Say I have a column in a dataframe that has some numbers and some non-numbers
>> df['foo']
0 0.0
1 103.8
2 751.1
3 0.0
4 0.0
5 -
6 -
7 0.0
8 -
9 0.0
Name: foo, Length: 9, dtype: object
如何将此列转换为 np.float
,并将其他所有非浮动列转换为 NaN
?
How can I convert this column to np.float
, and have everything else that is not float convert it to NaN
?
当我尝试时:
>> df['foo'].astype(np.float)
或
>> df['foo'].apply(np.float)
我得到 ValueError: could not convert string to float: -
推荐答案
In pandas 0.17.0
convert_objects
引发警告:
In pandas 0.17.0
convert_objects
raises a warning:
FutureWarning:convert_objects 已弃用.使用数据类型特定转换器 pd.to_datetime、pd.to_timedelta 和 pd.to_numeric.
FutureWarning: convert_objects is deprecated. Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.
您可以使用 pd.to_numeric
方法并将其应用于带有 arg coerce
的数据帧.
You could use pd.to_numeric
method and apply it for the dataframe with arg coerce
.
df1 = df.apply(pd.to_numeric, args=('coerce',))
或者更恰当:
df1 = df.apply(pd.to_numeric, errors='coerce')
编辑
以上方法只对pandas版本>=0.17.0
有效,来自文档 Pandas 0.17.0 的新功能:
The above method is only valid for pandas version >= 0.17.0
, from docs what's new in pandas 0.17.0:
pd.to_numeric 是一个将字符串强制转换为数字的新函数(可能使用强制转换)(GH11133)
pd.to_numeric is a new function to coerce strings to numbers (possibly with coercion) (GH11133)
这篇关于Pandas:转换为数字,必要时创建 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!