我有一个数据框 df1
id value
1 100
2 100
3 100
4 100
5 100
我有另一个数据框 df2
id value
2 50
5 30
我想用 df1 中的值替换 df2 中 id 的这些值。
最终修改后的df1:
id value
1 100
2 50
3 100
4 100
5 30
我将循环运行它。 i'e df2,将不时更改(df1,外部循环)
改变价值观的最好方法是什么?
Use combine_first http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.combine_first.html, 但首先set_index http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html by id
同时DataFrame
s:
Notice: id
列于df2
必须是独一无二的。
df = df2.set_index('id').combine_first(df1.set_index('id')).reset_index()
print (df)
id value
0 1 100.0
1 2 50.0
2 3 100.0
3 4 100.0
4 5 30.0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)