我在熊猫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。具有列的
p_id score
123 1.3
456 4.6
789 0.4
987 1.1
654 3.2
我必须填写所有所有的分数p_id_y which is NaN
与相应的分数p_id
在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
有什么想法要实现吗?我当时想用这个
df['score'] = df['score'].fillna(something)
我认为您可以使用combine_first
或fillna
,但首先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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句