为什么Pandas .fillna()不填充DataFrame中的值?

qmelpv7a  于 2023-11-15  发布在  其他
关注(0)|答案(3)|浏览(133)

我在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)

字符串
返回以下内容:

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


而以下:

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)


返回以下内容:

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


因此,当我使用.fillna()时,它用0填充Series值,而不是DataFrame值。我在DataFrame中用0代替null值时遗漏了什么?

ekqde3dh

ekqde3dh1#

它与调用fillna()函数的方式有关。
如果执行inplace=True(参见下面的代码),它们将被填充到位并覆盖原始 Dataframe 。

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

字符串

r8xiu3jd

r8xiu3jd2#

正如你在documentation中看到的,方法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

字符串

ee7vknir

ee7vknir3#

这样一个有趣的事件与我在一起,列仍然显示nan后fillna,最后发现,他们被读为“nan”字符串,所以我用替换为“nan”到“0”。我希望这有助于有人!

相关问题