python - 减去索引 - TypeError : cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index' >

标签 python pandas

我有两个巨大的数据框 我正在合并它们,但我不想有重复的列,因此我通过减去它们来选择列:

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

我收到此错误(在第一行):

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)

有人知道这意味着什么或者做同样事情的替代方法吗?

最佳答案

要查找索引的差异,有 difference (您已经在使用)。您无法通过 - 减去 - 该错误告诉您该女孩不支持此操作。

要查找 df_fin 中不在 df_peers 中的所有列,您可以使用

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

如果您也想从此 cnpj 中删除,您可以使用

cols_to_use=df_fin.columns.difference(df_peers.columns).difference(['cnpj'])
<小时/>

编辑

如果你想获得不重复的列的并集(按顺序),你可以使用

from collections import OrderedDict

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

关于python - 减去索引 - TypeError : cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index' >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50174127/

相关文章:

python - 创建一个允许用户选择日期过滤的 View /模板,其中包括子对象的总计?

python - 在 subprocess.call() 中使用带反引号的命令替换

python - 使 tkinter 文本小部件适合窗口

Python Pandas 数据框分配

python - 有没有办法将 `apply` 函数应用于数据帧的一列,同时保持其他列固定?

python - 为什么使用 urllib2 打开 url 时出现乱码?

python - 如何控制 seaborn 条形图中条形之间的空白?

python - 分组依据和过滤器

python - Pandas 计算行的增长百分比

python - 如何创建一个列来跟踪另一列中的值在 pandas 中的该行之前出现的次数