在 Pandas DataFrame 中对空值使用 None 而不是 np.nan

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

Use None instead of np.nan for null values in pandas DataFrame

pythonpandasdataframe

提问by J Jones

I have a pandas DataFrame with mixed data types. I would like to replace all null values with None (instead of default np.nan). For some reason, this appears to be nearly impossible.

我有一个混合数据类型的 Pandas DataFrame。我想用无(而不是默认的 np.nan)替换所有空值。出于某种原因,这似乎几乎是不可能的。

In reality my DataFrame is read in from a csv, but here is a simple DataFrame with mixed data types to illustrate my problem.

实际上,我的 DataFrame 是从 csv 读入的,但这里有一个带有混合数据类型的简单 DataFrame 来说明我的问题。

df = pd.DataFrame(index=[0], columns=range(5))
df.iloc[0] = [1, 'two', np.nan, 3, 4] 

I can't do:

我不能这样做:

>>> df.fillna(None)
ValueError: must specify a fill method or value

nor:

也不:

>>> df[df.isnull()] = None
TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

nor:

也不:

>>> df.replace(np.nan, None)
TypeError: cannot replace [nan] with method pad on a DataFrame

I used to have a DataFrame with only string values, so I could do:

我曾经有一个只有字符串值的 DataFrame,所以我可以这样做:

>>> df[df == ""] = None

which worked. But now that I have mixed datatypes, it's a no go.

哪个有效。但是现在我有混合数据类型,这是不行的。

For various reasons about my code, it would be helpful to be able to use None as my null value. Is there a way I can set the null values to None? Or do I just have to go back through my other code and make sure I'm using np.isnan or pd.isnull everywhere?

由于我的代码的各种原因,能够使用 None 作为我的空值会很有帮助。有没有办法可以将空值设置为无?或者我是否只需要返回我的其他代码并确保我在任何地方都使用 np.isnan 或 pd.isnull ?

回答by piRSquared

Use pd.DataFrame.where
Uses dfvalue when condition is met, otherwise uses None

Usepd.DataFrame.where满足条件时
使用df值,否则使用None

df.where(df.notnull(), None)

enter image description here

在此处输入图片说明