pandas :用字典引用另一列填充NaN值 [英] Pandas: fill in NaN values with dictionary references another column
本文介绍了 pandas :用字典引用另一列填充NaN值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一本像这样的字典
dict = {'b' : '5', 'c' : '4'}
我的数据框看起来像这样
My dataframe looks something like this
A B
0 a 2
1 b NaN
2 c NaN
是否有一种方法使用从A列到B列的字典映射来填充NaN值,同时保留其余的列值?
Is there a way to fill in the NaN values using the dictionary mapping from columns A to B while keeping the rest of the column values?
推荐答案
您可以在fillna内映射字典值
You can map dict values inside fillna
df.B = df.B.fillna(df.A.map(dict))
print(df)
A B
0 a 2
1 b 5
2 c 4
这篇关于 pandas :用字典引用另一列填充NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文