pandas fillna当前只能用dict/Series逐列填充 [英] pandas fillna Currently only can fill with dict/Series column by column

查看:257
本文介绍了 pandas fillna当前只能用dict/Series逐列填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下df

A
1.0
2.0
3.0
NaN

我试图fillna用字符串not existed替换NaN.

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

如果我使用replace,它将起作用,

if I use replace, it will work,

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

我想知道为什么会这样.

I am wondering why is that.

推荐答案

对我来说,删除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

这篇关于 pandas fillna当前只能用dict/Series逐列填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆