Pandas.fillna()未在Python 3的数据帧中填充值
我在Python 3中运行Pandas,我注意到以下几点:Pandas.fillna()未在Python 3的数据帧中填充值,python,pandas,Python,Pandas,我在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
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
返回以下内容:
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
同时:
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
返回以下内容:
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填充序列值,而不是数据帧值。这是Python3的问题吗?否则,我在这里缺少什么来在数据帧中用0代替空值?谢谢 如中所述,方法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
这与调用
fillna()
函数的方式有关
如果您执行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]:
不是这里发生的事情,但可能会帮助某些人,如果数据类型不是某个数字,则不能将df.fillna与df.mean一起使用(用列平均值替换缺少的值)。听起来很明显,但df.mean()本身仍然有效。