robert
robert

Reputation: 2027

Pandas add column from one dataframe to another based on a join

Assume I have 2 dataframes. I want to add a column of dataframe 1 to dataframe 2 based on a column lookup. If the join is not possible, I want in the extra column a certain constant (so I can filter for that).

Graphically:

enter image description here

Code:

import pandas as pd
import numpy as np

data = np.array([['','Col1','Col2'],
                ['Row1','2','TWO'],
                ['Row2','1','ONE']]
            )

data2 = np.array([['','Col3','Col4'],
                ['Row1','1','T1'],
                ['Row2','2','T2'],
                ['Row3','3','T3']]
            )

df = pd.DataFrame(data=data[1:,1:],
                  index=data[1:,0],
                  columns=data[0,1:])

df2 = pd.DataFrame(data=data2[1:,1:],
                  index=data2[1:,0],
                  columns=data2[0,1:])

result_df = df2 + join Col2 based on df2.Col3 = df.Col1. Add certain string constant if join fails. 

print(df)
print(df2)
print(result_df)

Upvotes: 18

Views: 29796

Answers (2)

Tushar Gupta
Tushar Gupta

Reputation: 281

You can also try to use pd.merge():

df3 = pd.merge(df2, df[['Col1','Col2']], left_on='Col3', right_on='Col1', how='left')

Upvotes: 5

jezrael
jezrael

Reputation: 862481

Use join or map:

df = df2.join(df.set_index('Col1'), on='Col3')
print (df)
     Col3 Col4 Col2
Row1    1   T1  ONE
Row2    2   T2  TWO
Row3    3   T3  NaN

df2['Col2'] = df2['Col3'].map(df.set_index('Col1')['Col2'])
print (df2)
     Col3 Col4 Col2
Row1    1   T1  ONE
Row2    2   T2  TWO
Row3    3   T3  NaN

Upvotes: 20

Related Questions