简体   繁体   English

在 pandas DataFrame 中使用 None 而不是 np.nan 作为空值

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

I have a pandas DataFrame with mixed data types.我有一个混合数据类型的 pandas DataFrame。 I would like to replace all null values with None (instead of default np.nan).我想用 None 替换所有空值(而不是默认的 np.nan)。 For some reason, this appears to be nearly impossible.出于某种原因,这似乎几乎是不可能的。

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.由于我的代码的各种原因,能够使用 None 作为我的 null 值会很有帮助。 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?还是我只需要返回我的其他代码并确保我在任何地方都使用 np.isnan 或 pd.isnull ?

Use pd.DataFrame.where 使用pd.DataFrame.where
Uses df value when condition is met, otherwise uses None 满足条件时使用df值,否则使用None

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

在此输入图像描述

Expanding on the accpeted answer.. When you also need to catch NaN values within numeric dtype columns, you may need to change dtype to object first:扩展接受的答案.. 当您还需要在数字 dtype 列中捕获NaN值时,您可能需要先将 dtype 更改为object

df.astype(object).where(df.notna(), None)

as per original reply by @BENNY根据@BENNY 的原始回复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用np.NaN替换熊猫数据框中的缺失值(以字符串形式给出) - Replace missing values (given as strings) in pandas dataframe by np.NaN Pandas dataframe 中 np.nan 的计数 - Count number of np.nan in a Pandas dataframe 用np.nan替换空值 - Replacing null values with np.nan 对熊猫数据框使用 to_excel(...) 函数时,如何将空字符串、np.nan 和 None 分开? - How to separate between empty string, np.nan and None when using to_excel(...) function for pandas dataframe? 为什么更改一个np.nan值会更改熊猫数据框中的所有nan值? - Why does changing one `np.nan` value change all of the nan values in pandas dataframe? 当值以'_h'结尾时,用np.nan替换pandas DataFrame值。 - Replacing a pandas DataFrame value with np.nan when the values ends with '_h' 在pandas数据帧中写一个用户定义的fillna函数,用条件填充np.nan不同的值 - Write a user defined fillna function in pandas dataframe to fill np.nan different values with conditions 计算其中带有np.nan的熊猫数据框的平均值的最佳方法是什么? - What is the best way to calculate the mean of the values of a pandas dataframe with np.nan in it? 尝试在Pandas数据框中添加列时,为什么会得到np.NaN值? - Why am I getting np.NaN values when trying to add a column to a Pandas dataframe? 无法使用系列设置 pandas 列值,而是将所有内容设置为 np.nan - cannot set pandas column values using series, sets everything to np.nan instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM