Python 熊猫 fillna 不工作

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

pandas fillna not working

pythonpandas

提问by Ginger

I have a dataframe with nans in it:

我有一个包含 nans 的数据框:

>>>df.head()
Out[1]: 
            JPM US SMALLER COMPANIES C ACC
1990-01-02                             NaN
1990-01-03                             NaN
1990-01-04                             NaN
1990-01-05                             NaN
1990-01-08                             NaN

I have another dataframe with values in it:

我有另一个包含值的数据框:

>>>t.head()
Out[1]: 
1990-01-02    51.95
1990-01-03    52.63
1990-01-04    53.04
1990-01-05    52.07
1990-01-08    51.73
Name: JPM US SMALLER COMPANIES C ACC, dtype: float64

Unfortunately, df.fillna does not appear to be working for me:

不幸的是, df.fillna 似乎对我不起作用:

>>>df.fillna( t ).head()
Out[1]: 
            JPM US SMALLER COMPANIES C ACC
1990-01-02                             NaN
1990-01-03                             NaN
1990-01-04                             NaN
1990-01-05                             NaN
1990-01-08                             NaN

[5 rows x 1 columns]

Why is this happening? I am on pandas 0.13.1

为什么会这样?我在熊猫 0.13.1

回答by zinking

you need inplace

你需要 inplace

df[1].fillna(0, inplace=True)

回答by Ravikant Singh

You need to assign the value df = df.fillna( t )

您需要分配值 df = df.fillna( t )

回答by E.Zolduoarrati

You have two options:

您有两个选择:

1) Specific for each column

1) 特定于每一列

cols_fillna = ['column1','column2','column3']
# replace 'NaN' with zero in these columns
 for col in cols_fillna:
     df[col].fillna(0,inplace=True)
     df[col].fillna(0,inplace=True)

2) For the entire dataframe

2)对于整个数据帧

df = df.fillna(0)