aabujamra
aabujamra

Reputation: 4636

Key error when joining dfs in Pandas

I have a dataframe with these columns:

df1:

Index(['cnpj', '#CNAE', 'Estado', 'Capital_Social', '#CNAEpai', '#CNAEvo',
   '#CNAEbisavo', 'Porte'],
  dtype='object')

I have another dataframe with these columns:

df2:

Index(['#CNAEpai', 'ROA_t12_Peers_CNAEpai', 'MgBruta_t12_Peers_CNAEpai',
   'MgEBITDA_t12_Peers_CNAEpai', 'LiqCorrente_t12_Peers_CNAEpai',
   'Crescimento_t12_Peers_CNAEpai', 'MgLucro_t12_Peers_CNAEpai',
   'Custo/Receita_t12_Peers_CNAEpai', 'Passivo/EBITDA_t12_Peers_CNAEpai',
   'ROE_t12_Peers_CNAEpai', 'RFinanceiro/Receita_t12_Peers_CNAEpai',
   'cnpj_t12_Peers_CNAEpai', 'LiqGeral_t12_Peers_CNAEpai'],
  dtype='object')

I'm trying to join them, using this line:

df1=df1.join(df2,on=['#CNAEpai'],how='left',rsuffix='_bbb')

But I'm getting this error:

KeyError: '#CNAEpai'

Since #CNAEpai is a column in both dfs that shouldn't be happening right? What's going on?

Upvotes: 3

Views: 393

Answers (1)

jpp
jpp

Reputation: 164843

As @root indicated, pd.DataFrame.join joins index-on-index or index-on-column, but not column-on-column.

To join on column(s), use pd.DataFrame.merge:

df1 = df1.merge(df2, on='#CNAEpai', how='left', rsuffix='_bbb')

Upvotes: 1

Related Questions