Python Forum

Full Version: How do I merge df1 and df2 by two columns?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, everyone! Help-me, please...

How do I merge df1 and df2 by two columns (fiels) at clausula on?

For example:

dfUltStatus = pd.merge(dfUltStatus, dfDescStatus,
on=['CODIGO_STATUS','SUB_CODIGO_STATUS'], how = 'left')
The object is merge the two data frames through these two fields to bring the description field.

Tks by your help, friends!
>>> import pandas as pd
>>> df1 = pd.DataFrame({'ID1': [1,2,3],'ID2': ['aa','bb','cc'],'Desc1':['d1','d2','d3']})
>>> df2 = pd.DataFrame({'ID1': [1,2],'ID2': ['aa','bb'],'Desc2':['d11','d12']})
>>> df_left = pd.merge(df1, df2, on=['ID1','ID2'], how='left')
>>> df_left
   ID1 ID2 Desc1 Desc2
0    1  aa    d1   d11
1    2  bb    d2   d12
2    3  cc    d3   NaN
https://pandas.pydata.org/pandas-docs/st...merge.html