Skip to content Skip to sidebar Skip to footer

Merging Two Pandas Dataframes On Multiple Columns

I have two dataframes: >>> df1 [Output]: col1 col2 col3 col4 a abc 10 str1 b abc 20 str2 c def 20 s

Solution 1:

Use .merge by subselecting the correct columns and using col1 & col2 as key columns:

df1[['col1', 'col2']].merge(df2[['col1', 'col2', 'col5']], on=['col1', 'col2'])

  col1 col2  col5
0    a  abc  str5
1    b  abc  str5
2    c  def  str7
3    d  abc  str6

Solution 2:

df_merged = pd.DataFrame()
df_merged['col1'] = df1['col1'][0:3]
df_merged['col2'] = df1['col2'][0:3]
df_merged['col5'] = df2['col5'][0:3]

Does that help with what you're looking for?


Post a Comment for "Merging Two Pandas Dataframes On Multiple Columns"