Pandas .fillna() 不填充 Python 3 中 DataFrame 中的值

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

Pandas .fillna() not filling values in DataFrame in Python 3

pythonpandas

提问by cenveoanalyst

I'm running Pandas in Python 3 and I noticed that the following:

我在 Python 3 中运行 Pandas,我注意到以下内容:

import pandas as pd
import numpy as np
from pandas import DataFrame
from numpy import nan

df = DataFrame([[1, nan], [nan, 4], [5, 6]])

print(df)

df2 = df
df2.fillna(0)

print(df2)

Returns the following:

返回以下内容:

 0   1
0   1 NaN
1 NaN   4
2   5   6
    0   1
0   1 NaN
1 NaN   4
2   5   6

While the following:

而以下:

import pandas as pd
import numpy as np
from pandas import Series
from numpy import nan

sr1 = Series([1,2,3,nan,5,6,7])

sr1.fillna(0)

Returns the following:

返回以下内容:

0    1
1    2
2    3
3    0
4    5
5    6
6    7
dtype: float64

So it's filling in Series values but not DataFrame values with 0 when I use .fillna(). Is this a problem with Python 3? Otherwise, what am I missing here to get 0s in place of null values in DataFrames? Thanks!

因此,当我使用 .fillna() 时,它使用 0 填充系列值而不是 DataFrame 值。这是 Python 3 的问题吗?否则,我在这里缺少什么来获取 0 代替 DataFrame 中的空值?谢谢!

回答by Alberto Bonsanto

As you can read in the documentation, the method fillna(newValue)returns another DataFramelike the previous one, but with the nanvalues replaced by the new value.

正如您在文档中所读到的,该方法fillna(newValue)返回另一个DataFrame与前一个类似的方法,但nan值被新值替换。

df = DataFrame([[1, nan], [nan, 2], [3, 2]])
df2 = df.fillna(0)

print(df2)
# Outputs
#   0 1
# 0 1 0
# 1 0 2
# 2 3 2

print(df)
# Outputs (The previous one isn't modified)
#   0   1
# 0 1   nan
# 1 nan 2
# 2 3   2

回答by ericmjl

It has to do with the way you're calling the fillna()function.

它与您调用fillna()函数的方式有关。

If you do inplace=True(see code below), they will be filled in place and overwrite your original data frame.

如果您这样做inplace=True(请参阅下面的代码),它们将被填充到位并覆盖您的原始数据框。

In [1]: paste
import pandas as pd
import numpy as np
from pandas import DataFrame
from numpy import nan

df = DataFrame([[1, nan], [nan, 4], [5, 6]])
## -- End pasted text --

In [2]: 

In [2]: df
Out[2]: 
    0   1
0   1 NaN
1 NaN   4
2   5   6

In [3]: df.fillna(0)
Out[3]: 
   0  1
0  1  0
1  0  4
2  5  6

In [4]: df2 = df

In [5]: df2.fillna(0)
Out[5]: 
   0  1
0  1  0
1  0  4
2  5  6

In [6]: df2  # note how this is unchanged.
Out[6]: 
    0   1
0   1 NaN
1 NaN   4
2   5   6

In [7]: df.fillna(0, inplace=True)  # this will replace the values.

In [8]: df
Out[8]: 
   0  1
0  1  0
1  0  4
2  5  6

In [9]: