I am merging two data frames with pandas. I would like to avoid that, when joining, the output includes the join column of the right table.
Example:
JavaScript
x
10
10
1
import pandas as pd
2
3
age = [['tom', 10], ['nick', 15], ['juli', 14]]
4
df1 = pd.DataFrame(age, columns = ['Name', 'Age'])
5
6
toy = [['tom', 'GIJoe'], ['nick', 'car']]
7
df2 = pd.DataFrame(toy, columns = ['Name_child', 'Toy'])
8
9
df = pd.merge(df1,df2,left_on='Name',right_on='Name_child',how='left')
10
df.columns
will give the output Index(['Name', 'Age', 'Name_child', 'Toy'], dtype='object')
. Is there an easy way to obtain Index(['Name', 'Age', 'Toy'], dtype='object')
instead? I can drop the column afterwards of course like this del df['Name_child']
, but I’d like my code to be as short as possible.
Advertisement
Answer
Based on @mgc comments, you don’t have to rename the columns of df2. Just you pass df2 to merge
function with renamed columns. df2 column names will remain as it is.
JavaScript
1
14
14
1
df = pd.merge(df1,df2.rename(columns={'Name_child': 'Name'}),on='Name', how='left')
2
3
df
4
Name Age Toy
5
0 tom 10 GIJoe
6
1 nick 15 car
7
2 juli 14 NaN
8
9
df.columns
10
Index(['Name', 'Age', 'Toy'], dtype='object')
11
12
df2.columns
13
Index(['Name_child', 'Toy'], dtype='object')
14