[英]How to update one dataframe using values from another dataframe in pandas
I have two df
s, df1
is like, 我有两个
df
, df1
就像,
primary_key code amount
220492763 763 32.41
213274768 764 23.41
226835769 766 88.41
224874836 7766 100.31
219074759 74836 111.33
df2
is like, df2
就像
primary_key code amount
213274768 764 24.41
224874836 7766 101.31
217774816 768 123.43
222176762 798 111.44
219374759 24774 134.56
I like to use df2
to update df_1
based on the same primary_key
, and for the rest of rows in df2
, append them to the end of df1
, so the result looks like, 我喜欢使用
df2
来基于相同的primary_key
更新df_1
,对于df2
的其余行,请将它们附加到df1
的末尾,所以结果看起来像是,
primary_key code amount
220492763 763 32.41
213274768 764 24.41
226835769 766 88.41
224874836 7766 101.31
219074759 74836 111.33
217774816 768 123.43
222176762 798 111.44
219374759 24774 134.56
have tried to use 尝试使用
df1.set_index('primary_key').combine_first(df2.set_index('primary_key')).reset_index()
but the two df
s mixed together, I am wondering how to fix it. 但是两个
df
混合在一起,我想知道如何解决它。
Using combine_first
使用
combine_first
yourdf=df2.set_index('primary_key').combine_first(df1.set_index('primary_key')).reset_index()
yourdf
Out[287]:
primary_key code amount
0 213274768 764.0 24.41
1 217774816 768.0 123.43
2 219074759 74836.0 111.33
3 219374759 24774.0 134.56
4 220492763 763.0 32.41
5 222176762 798.0 111.44
6 224874836 7766.0 101.31
7 226835769 766.0 88.41
Update adding the order 更新添加订单
idx=pd.concat([df1.primary_key,df2.primary_key]).drop_duplicates()
yourdf=df2.set_index('primary_key').combine_first(df1.set_index('primary_key')).reindex(idx).reset_index()
yourdf
Out[293]:
primary_key code amount
0 220492763 763.0 32.41
1 213274768 764.0 24.41
2 226835769 766.0 88.41
3 224874836 7766.0 101.31
4 219074759 74836.0 111.33
5 217774816 768.0 123.43
6 222176762 798.0 111.44
7 219374759 24774.0 134.56
Use pd.concat
, drop_duplicates
, and reindex
: 使用
pd.concat
, drop_duplicates
和reindex
:
idx=pd.concat([df1.primary_key,df2.primary_key]).drop_duplicates()
pd.concat([df2,df1]).drop_duplicates('primary_key').set_index('primary_key').reindex(idx).reset_index()
Output: 输出:
primary_key code amount
0 220492763 763 32.41
1 213274768 764 24.41
2 226835769 766 88.41
3 224874836 7766 101.31
4 219074759 74836 111.33
5 217774816 768 123.43
6 222176762 798 111.44
7 219374759 24774 134.56
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.