Merge columns of two dataframes without duplicate rows

Hello I want to merge two dataframes based on a matching value of column . When I did this, it resulted in duplicate rows.

My dataframes looks like:

df1:
  e_name   p1   p2  p3    
  e01      10   12  15
  e02      12   18  25
  e03      16   14  09

 df2:
  e_name   mean       
  e01      09 
  e02      11
  e03      15

Expected Output:

 out1:
      e_name   p1   p2  p3   mean 
     e01      10   12  15   09  
     e02      12   18  25   11
     e03      16   14  09   15

My code:

  #Solution 1:
  out1 = pd.merge(df1, df2, how='inner', on='e_name')

   #Solution 2:
   out2 = summary_stats.merge(df1, df2, left_on='e_name', right_on='e_name')

Both the solutions return duplicate rows:

 e_name   p1   p2  p3   mean 
 e01      10   12  15   09
 e01      10   12  15   09  
 e02      12   18  25   11
 e02      12   18  25   11
 e03      16   14  09   15
 e03      16   14  09   15

How do I get a solution without duplicate rows?

728x90

1 Answers Merge columns of two dataframes without duplicate rows

I think you need remove duplicates in second DataFrame by drop_duplicates per column e_name:

out1 = pd.merge(df1, df2.drop_duplicates('e_name'), on='e_name')

Or in both:

out1 = pd.merge(df1.drop_duplicates('e_name'), df2.drop_duplicates('e_name'), on='e_name')

4 months ago