简体   繁体   English

如何使用熊猫中另一个数据框的值更新一个数据框

[英]How to update one dataframe using values from another dataframe in pandas

I have two df s, df1 is like, 我有两个dfdf1就像,

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.concatdrop_duplicatesreindex

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.

相关问题 如何使用来自另一个数据帧(熊猫)的值更新空的 dataframe 值? - How to update the empty dataframe values with values from another dataframe(Pandas)? 使用来自另一个数据框的值更新熊猫数据框 - Update pandas dataframe with values from another dataframe 如何使用pandas从另一个数据框中查找值? - How to find values from one dataframe in another using pandas? 使用 pandas 从一个 dataframe 在另一个 dataframe 中搜索值 - searching values from one dataframe in another dataframe using pandas 如何使用来自另一个 dataframe 的新列更新 Pandas dataframe? - How to update Pandas dataframe with a new column using values from another dataframe? pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values 用另一个数据框中的值替换一个熊猫数据框中的值 - Replacing values in one pandas dataframe with values from another dataframe Python Pandas:从另一个数据框更新数据框值 - Python Pandas: update dataframe values from another dataframe 从另一个 dataframe 更新 pandas dataframe 中的特定值 - Update particular values in a pandas dataframe from another dataframe 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM