Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas fillna Currently only can fill with dict/Series column by column

I have the following df,

A
1.0
2.0
3.0
NaN

I tried to fillna to replace NaN with a string not existed.

df.fillna(value={'A': 'not existed'}, axis=1, inplace=True)

but I got the following error,

NotImplementedError: Currently only can fill with dict/Series column by column

if I use replace, it will work,

df['A'].replace(np.nan, 'not existed')

I am wondering why is that.

like image 565
daiyue Avatar asked Oct 25 '25 23:10

daiyue


2 Answers

For me working removing axis=1:

print (df)
     A    B
0  NaN  NaN
1  2.0  7.0
2  3.0  8.0
3  NaN  7.0

df.fillna({'A': 'not existed'}, inplace=True)
print (df)
             A    B
0  not existed  NaN
1            2  7.0
2            3  8.0
3  not existed  7.0

df.fillna({'A': 'not existed', 'B':'nwwww'}, inplace=True)
print (df)
             A      B
0  not existed  nwwww
1            2      7
2            3      8
3  not existed      7
like image 155
jezrael Avatar answered Oct 27 '25 13:10

jezrael


When you use fillna on a DataFrame you should pass a dictionary with values for each column you want to fill as described here.

In any case, your error is related with the way in which you use axis, take a look to this working example:

import pandas

x = pandas.DataFrame({
    'x_1': [0, 1, 2, 3, 0, 1, 2, None, ],},
    index=[0, 1, 2, 3, 4, 5, 6, 7])

x.fillna(value={'x_1': 'not existed',}, axis=0, inplace=True)
like image 24
Pierluigi Avatar answered Oct 27 '25 14:10

Pierluigi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!