本文介绍了 pandas .fillna()未在Python 3中填充DataFrame中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Python 3中运行Pandas,我注意到以下内容:
I'm running Pandas in Python 3 and I noticed that the following:
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()时,它填充的是Series值,而不是0的DataFrame值.这是Python 3的问题吗?否则,我在这里缺少什么来代替DataFrames中的空值以获得0?谢谢!
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()
函数的方式有关.
It has to do with the way you're calling the fillna()
function.
如果执行inplace=True
(请参见下面的代码),它们将被填充到位并覆盖原始数据框.
If you do inplace=True
(see code below), they will be filled in place and overwrite your original data frame.
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]:
这篇关于 pandas .fillna()未在Python 3中填充DataFrame中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!