aabujamra
aabujamra

Reputation: 4636

Subtracting indexes - TypeError: cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index'>

I have two huge dataframes I'm merging them, but I don't want to have repeated columns, so I'm selecting columns by subtracting them:

cols_to_use=df_fin.columns-df_peers.columns.difference(['cnpj'])
df=df_peers.merge(df_fin[cols_to_use],on='cnpj',how='left')

I'm getting this error (in the first line):

TypeError: cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index'>

df_fin.columns:
Index(['cnpj', 'ano', 'id', 'unit', 'period', 'Ativo Circulante',
   'Ativo Nao-Circulante', 'Ativo Total', 'Custos', 'Depreciacao',
   'Despesas Financeiras', 'EBITDA', 'Lucro Antes do Resultado Financeiro',
   'Lucro Antes dos Impostos', 'Lucro Bruto', 'Lucro Liquido',
   'Passivo Circulante', 'Passivo Nao-Circulante', 'Passivo Total',
   'Patrimonio Liquido', 'Receita Liquida', 'Receitas Financeiras',
   'Crescimento', 'MgLucro', 'Custo/Receita', 'MgBruta', 'MgEBITDA',
   'Passivo/EBITDA', 'LiqCorrente', 'LiqGeral', 'Resultado Financeiro',
   'RFinanceiro/Receita', 'ROA', 'ROE', 'Razao_social', 'Nome_Fantasia',
   'Estado', 'Cidade', 'CNAE', '#CNAE', 'Capital_Social', 'Data_fundacao',
   'CEP', 'Bairro', 'Rua', 'Numero', 'Complemento_endereco',
   'Natureza_Juridica', 'Telefone', 'email', 'last_revenue_normalized',
   'last_revenue_year', 'situacao_cadastral', 'situacao_especial',
   'Unnamed: 0'],
  dtype='object')

df_peers.columns:
Index(['Unnamed: 0', 'cnpj', 'Razao_social', 'Nome_Fantasia', 'Estado',
   'Cidade', 'CNAE', '#CNAE', 'Capital_Social', 'Data_fundacao',
   ...
   'Custo/Receita_t44_Peers_CNAEbisavo_estado_porte',
   'MgBruta_t44_Peers_CNAEbisavo_estado_porte',
   'Crescimento_t44_Peers_CNAEbisavo_estado_porte',
   'cnpj_t44_Peers_CNAEbisavo_estado_porte',
   'MgEBITDA_t44_Peers_CNAEbisavo_estado_porte',
   'Passivo/EBITDA_t44_Peers_CNAEbisavo_estado_porte',
   'ROE_t44_Peers_CNAEbisavo_estado_porte',
   'RFinanceiro/Receita_t44_Peers_CNAEbisavo_estado_porte',
   'ROA_t44_Peers_CNAEbisavo_estado_porte',
   'MgLucro_t44_Peers_CNAEbisavo_estado_porte'],
  dtype='object', length=250)

Does someone know what that could mean or an alternative way of doing the same thing?

Upvotes: 3

Views: 8151

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76386

To find the difference in indexes there is difference (which you're already using). You cannot subtract columns via - - the error is telling you that the lass does not support this operation.

To find all columns in df_fin that are not in df_peers, you can use

cols_to_use=df_fin.columns.difference(df_peers.columns)

If you would like to remove from this cnpj as well, you could use

cols_to_use=df_fin.columns.difference(df_peers.columns).difference(['cnpj'])

Edit

If you want to get the union of columns (in order) without duplicates, you can use

from collections import OrderedDict

list(OrderedDict.fromkeys(list(df_fin.columns) + list(df_peers.columns)))

Upvotes: 3

Related Questions