简体   繁体   English

从其他数据框 pandas 填充数据框中列的 NAN 值

[英]Fill NAN values of a column in dataframe from other dataframe pandas

i have a table in pandas df我在 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

also i have another dataframe df2.我还有另一个数据框df2。 which has the column's其中有列的

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

i have to fill all the scores for all p_id_y which is NaN with the respective score of p_id in df2 .我必须用df2p_id的相应分数填充所有p_id_y which is NaN

my final output should be.我的最终输出应该是。

      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)

I think you can use combine_first or fillna , but first set_index for align data:我认为您可以使用combine_firstfillna ,但首先set_index用于对齐数据:

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

use fillna and join使用fillnajoin

df.fillna(df[['p_id_y']].join(df2.set_index('p_id'), on='p_id_y'))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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