从其他数据帧 pandas 填充数据帧中列的 NAN 值 [英] Fill NAN values of a column in dataframe from other dataframe pandas

查看:53
本文介绍了从其他数据帧 pandas 填充数据帧中列的 NAN 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pandas df 中有一张桌子

i have a table in pandas df

      main_id       p_id_y       score
1       1            123        0.617523
0       2            456        0.617523
0       3            789        NaN
0       4            987        NaN
1       5            654        NaN

我还有另一个数据框 df2.其中有列的

also i have another dataframe df2. which has the column's

p_id   score
 123    1.3
 456    4.6
 789    0.4
 987    1.1
 654    3.2

我必须用 df2p_id 的相应分数填充所有 p_id_y 这是 NaN 的所有分数.

i have to fill all the scores for all p_id_y which is NaN with the respective score of p_id in df2.

我的最终输出应该是.

      main_id       p_id_y       score
1       1            123        0.617523
0       2            456        0.617523
0       3            789        0.4
0       4            987        1.1
1       5            654        3.2

任何想法如何实现?我想用这个

Any ideas how to achieve that? i was thinking to use this

df['score'] = df['score'].fillna(something)

推荐答案

我认为你可以使用 combine_firstfillna,但首先set_index 用于对齐数据:

I think you can use combine_first or fillna, but first set_index for align data:

df1 = df1.set_index('p_id_y')
df1['score'] = df1['score'].combine_first(df2.set_index('p_id')['score'])
#df1['score'] = df1['score'].fillna(df2.set_index('p_id')['score'])

print (df1.reset_index())
   p_id_y  main_id     score
0     123        1  0.617523
1     456        2  0.617523
2     789        3  0.400000
3     987        4  1.100000
4     654        5  3.200000

这篇关于从其他数据帧 pandas 填充数据帧中列的 NAN 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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