gpt4 book ai didi

python - 减去索引 - TypeError : cannot perform __sub__ with this index type:

转载 作者:行者123 更新时间:2023-12-01 01:56:07 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com