Python Forum
How do I merge df1 and df2 by two columns?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I merge df1 and df2 by two columns?
#1
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!
Reply
#2
>>> 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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020