简体   繁体   English

合并和覆盖 pandas df 中的值

[英]Merge and overwrite values in pandas df

I have two df's containing that I'm hoping to merge and overwrite specific values.我有两个包含我希望合并和覆盖特定值的df。 Using below, df1 and df2 will be merged and I'm hoping to overwrite the values in Group for certain rows.使用下面, df1df2将被合并,我希望覆盖Group中某些行的值。 Specifically, where Group is == Y , I want to use the values in df2 .具体来说,其中Group是 == Y ,我想使用df2中的值。 I've got a way but I don't think it's very efficient.我有办法,但我认为它不是很有效。

df1 = pd.DataFrame({      
    'Time' : [1,1,1,1,2,2,2,2,2],
    'Label' : ['A','B','C','D','A','B','C','D','E'],    
    'Group' : ['X','X','X','Y','X','Y','X','Y','X'],            
   })

df2 = pd.DataFrame({  
    'Time' : [1,2,2],        
    'Label' : ['D','B','D'],  
    'Group2' : ['Y1','Y3','Y2'],                                            
    })

df_out = pd.merge(df1,df2, how = 'outer')

out:出去:

   Time Label Group Group2
0     1     A     X    NaN
1     1     B     X    NaN
2     1     C     X    NaN
3     1     D     Y     Y1
4     2     A     X    NaN
5     2     B     Y     Y3
6     2     C     X    NaN
7     2     D     Y     Y2
8     2     E     X    NaN

I could then copy Group2 to Group excluding the NaN values and then drop Group2 but is there a more efficient method然后我可以将Group2复制到Group中,不包括 NaN 值,然后删除Group2但有没有更有效的方法

If I understand you right, you want to update df1['Group'] with df2['Group2'] values:如果我理解正确,您想用df2['Group2']值更新df1['Group']

df1["Group"].update(df1.merge(df2, on=["Time", "Label"], how="outer")["Group2"])
print(df1)

Prints:印刷:

   Time Label Group
0     1     A     X
1     1     B     X
2     1     C     X
3     1     D    Y1
4     2     A     X
5     2     B    Y3
6     2     C     X
7     2     D    Y2
8     2     E     X

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

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