ola1996
ola1996

Reputation: 57

Error caused by merging two tables in python

I want to merge two tables in one I have all clients with important metrics and in the second client_id with emails of clients. I was trying to do with this:

import pandas as pd
result = pd.merge(allclients, dataframe2[['owner_email','trk_hs_owner']], on='client_id')

I got error with client_id and I don't know what could cause this.

Upvotes: 0

Views: 178

Answers (3)

BALA
BALA

Reputation: 99

dataframe2 doesn't contain client_id column.If client_id column is there in dataframe2 you need to select that as well and if it is present with some different name then you need to specify left and right column name. Two possible solutions are:

If column name is same in both table:

pd.merge(allclients, dataframe2[['client_id','owner_email','trk_hs_owner']], on='client_id')

If column name is different in both table:

pd.merge(allclients, dataframe2[['diff_col_name','owner_email','trk_hs_owner']],left_on='client_id',right_on='diff_col_name')

Upvotes: 1

kjul
kjul

Reputation: 166

this statement selects only two columns of dataframe2:

dataframe2[['owner_email','trk_hs_owner']]

This drops the client_id column. Hence, you cannot join on client_id after.

Try:

pd.merge(allclients, dataframe2[['owner_email','trk_hs_owner', 'client_id']], on='client_id')

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148965

You are merging allclients that certainly has a client_id column with dataframe2[['owner_email','trk_hs_owner']] that cannot: you sliced it to only 2 columns 'owner_email','trk_hs_owner'.

You should use:

result = pd.merge(allclients, dataframe2[['client_id','owner_email','trk_hs_owner']],
                  on='client_id')

Upvotes: 2

Related Questions