简体   繁体   中英

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

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)

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!

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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