Python Pandas:转换为数字,必要时创建 NaN

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

Pandas: Converting to numeric, creating NaNs when necessary

pythonpandas

提问by Amelio Vazquez-Reina

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: could not convert string to float: -

采纳答案by Anton Protopopov

In pandas 0.17.0convert_objectsraises a warning:

在熊猫中0.17.0convert_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_numericmethod and apply it for the dataframe with arg coerce.

您可以使用pd.to_numericmethod 并将其应用于带有 arg 的数据帧coerce

df1 = df.apply(pd.to_numeric, args=('coerce',))

or maybe more appropriately:

或者也许更合适:

df1 = df.apply(pd.to_numeric, errors='coerce')

EDIT

编辑

The above method is only valid for pandas version >= 0.17.0, from docs what's new in pandas 0.17.0:

上述方法仅适用于 pandas 版本 >= 0.17.0,来自docs what's new in pandas 0.17.0

pd.to_numeric is a new function to coerce strings to numbers (possibly with coercion) (GH11133)

pd.to_numeric 是一个将字符串强制转换为数字的新函数(可能带有强制转换)(GH11133)

回答by Viktor Kerkez

First replace all the string values with None, to mark them as missing values and then convert it to float.

首先用 替换所有字符串值None,将它们标记为缺失值,然后将其转换为浮点数。

df['foo'][df['foo'] == '-'] = None
df['foo'] = df['foo'].astype(float)

回答by Andy Hayden

Use the convert_objectsSeries method (and convert_numeric):

使用convert_objectsSeries 方法(和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 方法。

回答by Amir Imani

You can simply use pd.to_numericand setting error to coercewithout using apply

您可以简单地使用pd.to_numeric并将错误设置为coerce不使用apply

df['foo'] = pd.to_numeric(df['foo'], errors='coerce')