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
How can I convert this column to np.float
, and have everything else that is not float convert it to NaN
?
如何将此列转换为np.float,并将其他所有不浮动的列转换为NaN?
When I try:
当我尝试:
>> df['foo'].astype(np.float)
or
>> df['foo'].apply(np.float)
I get ValueError: could not convert string to float: -
我得到ValueError:无法将字符串转换为float: -
4 个解决方案
#1
45
In pandas 0.17.0
convert_objects
raise a warning:
在pandas 0.17.0中,convert_objects会发出警告:
FutureWarning: convert_objects is deprecated. Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.
FutureWarning:不推荐使用convert_objects。使用特定于数据类型的转换器pd.to_datetime,pd.to_timedelta和pd.to_numeric。
You could use pd.to_numeric
method and apply it for the dataframe with arg coerce
.
您可以使用pd.to_numeric方法并将其应用于具有arg强制的数据帧。
df1 = df.apply(pd.to_numeric, args=('coerce',))
or may be in more appropriate way:
或者可能以更恰当的方式:
df1 = df.apply(pd.to_numeric, errors='coerce')
EDIT
That method only valid for pandas version >= 0.17.0
, from docs what's new in pandas 0.17.0:
该方法仅适用于pandas版本> = 0.17.0,来自docs pandas 0.17.0中的新内容:
pd.to_numeric is a new function to coerce strings to numbers (possibly with coercion) (GH11133)
pd.to_numeric是一个将字符串强制转换为数字的新函数(可能带有强制)(GH11133)
#2
31
Use the convert_objects
Series method (and convert_numeric
):
使用convert_objects Series方法(和convert_numeric):
In [11]: s
Out[11]:
0 103.8
1 751.1
2 0.0
3 0.0
4 -
5 -
6 0.0
7 -
8 0.0
dtype: object
In [12]: s.convert_objects(convert_numeric=True)
Out[12]:
0 103.8
1 751.1
2 0.0
3 0.0
4 NaN
5 NaN
6 0.0
7 NaN
8 0.0
dtype: float64
Note: this is also available as a DataFrame method.
注意:这也可用作DataFrame方法。
#3
7
First replace all the string values with None
, to mark them as missing values and then convert it to float.
首先用None替换所有字符串值,将它们标记为缺失值,然后将其转换为float。
df['foo'][df['foo'] == '-'] = None
df['foo'] = df['foo'].astype(float)
#4
4
You can simply use pd.to_numeric
and setting error to coerce
without using apply
您可以简单地使用pd.to_numeric并设置错误来强制而不使用apply
df['foo'] = pd.to_numeric(df['foo'], errors='coerce')
#1
45
In pandas 0.17.0
convert_objects
raise a warning:
在pandas 0.17.0中,convert_objects会发出警告:
FutureWarning: convert_objects is deprecated. Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.
FutureWarning:不推荐使用convert_objects。使用特定于数据类型的转换器pd.to_datetime,pd.to_timedelta和pd.to_numeric。
You could use pd.to_numeric
method and apply it for the dataframe with arg coerce
.
您可以使用pd.to_numeric方法并将其应用于具有arg强制的数据帧。
df1 = df.apply(pd.to_numeric, args=('coerce',))
or may be in more appropriate way:
或者可能以更恰当的方式:
df1 = df.apply(pd.to_numeric, errors='coerce')
EDIT
That method only valid for pandas version >= 0.17.0
, from docs what's new in pandas 0.17.0:
该方法仅适用于pandas版本> = 0.17.0,来自docs pandas 0.17.0中的新内容:
pd.to_numeric is a new function to coerce strings to numbers (possibly with coercion) (GH11133)
pd.to_numeric是一个将字符串强制转换为数字的新函数(可能带有强制)(GH11133)
#2
31
Use the convert_objects
Series method (and convert_numeric
):
使用convert_objects Series方法(和convert_numeric):
In [11]: s
Out[11]:
0 103.8
1 751.1
2 0.0
3 0.0
4 -
5 -
6 0.0
7 -
8 0.0
dtype: object
In [12]: s.convert_objects(convert_numeric=True)
Out[12]:
0 103.8
1 751.1
2 0.0
3 0.0
4 NaN
5 NaN
6 0.0
7 NaN
8 0.0
dtype: float64
Note: this is also available as a DataFrame method.
注意:这也可用作DataFrame方法。
#3
7
First replace all the string values with None
, to mark them as missing values and then convert it to float.
首先用None替换所有字符串值,将它们标记为缺失值,然后将其转换为float。
df['foo'][df['foo'] == '-'] = None
df['foo'] = df['foo'].astype(float)
#4
4
You can simply use pd.to_numeric
and setting error to coerce
without using apply
您可以简单地使用pd.to_numeric并设置错误来强制而不使用apply
df['foo'] = pd.to_numeric(df['foo'], errors='coerce')