Pandas combine columns with different names and avoid duplication

How can I combine two pandas DataFrames in two columns with different names and save one of the columns?

df1 = pd.DataFrame({'UserName': [1,2,3], 'Col1':['a','b','c']})
df2 = pd.DataFrame({'UserID': [1,2,3], 'Col2':['d','e','f']})
pd.merge(df1, df2, left_on='UserName', right_on='UserID')

It provides a DataFrame similar to this.

enter image description here

But it’s clear that I am merging on UserNameand UserIDso that they are the same. I want it to look like this. Are there any clean ways to do this?

enter image description here

Only the ways I can think of are either to rename the columns so that they are the same before the merge, or discard one of them after the merge. I would be happy if pandas automatically deleted one of them or I could do something like

pd.merge(df1, df2, left_on='UserName', right_on='UserID', keep_column='left')
+4
source share
2 answers

UserID , ?

pd.merge(df1, df2.set_index('UserID'), left_on='UserName', right_index=True)

#   Col1    UserName    Col2
# 0    a           1       d
# 1    b           2       e
# 2    c           3       f
+3

: , , left right external join, . , ,

1:

df2.columns = ['Col2', 'UserName']

pd.merge(df1, df2,on='UserName')
Out[67]: 
  Col1  UserName Col2
0    a         1    d
1    b         2    e
2    c         3    f

2:

pd.merge(df1, df2, left_on='UserName', right_on='UserID').drop('UserID', axis=1)
Out[71]: 
  Col1  UserName Col2
0    a         1    d
1    b         2    e
2    c         3    f
+4

Source: https://habr.com/ru/post/1657421/


All Articles