简体   繁体   English

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

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

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().因此,当我使用 .fillna() 时,它使用 0 填充系列值而不是 DataFrame 值。 Is this a problem with Python 3?这是 Python 3 的问题吗? Otherwise, what am I missing here to get 0s in place of null values in DataFrames?否则,我在这里缺少什么来获取 0 代替 DataFrame 中的空值? Thanks!谢谢!

As you can read in the documentation , the method fillna(newValue) returns another DataFrame like the previous one, but with the nan values 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

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]: 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM