Python/Pandas“column”不在索引中

2024-04-29 14:46:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,当我print(delivery.columns)时,我得到以下信息:

Index(['Complemento_endereço', 'cnpj', 'Data_fundação', 'Número',
   'Razão_social', 'CEP', 'situacao_cadastral', 'situacao_especial', 'Rua',
   'Nome_Fantasia', 'last_revenue_normalized', 'last_revenue_year',
   'Telefone', 'email', 'Capital_Social', 'Cidade', 'Estado',
   'Razão_social', 'name_bairro', 'Natureza_Jurídica', 'CNAE', '#CNAE',
   'CNAEs_secundários', 'Pessoas', 'percent'],
  dtype='object')

好吧,我们可以清楚地看到有一个列“Rua”。在

另外,如果我print(delivery.Rua)我得到一个完整的数据帧:

^{pr2}$

即使我写 if 'Rua' in delivery.columns: print('here I am'),它也会打印出“我在这里”。所以“Rua”实际上是存在的。在

好吧,在我得到这个代码后的第一行:

delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Rua','Número','Complemento_endereço',
                                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]

沃伊拉,我得到了一个奇怪的错误:

Traceback (most recent call last):
File "/file.py", line 45, in <module>
   'Telefone', 'email', 'Capital_Social', 'Cidade', 'Estado',
   'Razão_social', 'name_bairro', 'Natureza_Jurídica', 'CNAE', '#CNAE',
'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]
   'CNAEs_secundários', 'Pessoas', 'percent'],
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 1991, in __getitem__
  dtype='object')
return self._getitem_array(key)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2035, in _getitem_array
indexer = self.ix._convert_to_indexer(key, axis=1)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/indexing.py", line 1214, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['Rua'] not in index"

有人能帮忙吗?我开始觉得我疯了,而“Rua”是我困扰心灵的幻觉。在

编辑

我在错误行之前使用了以下代码:

delivery=pd.DataFrame()

for i in selection.index:
    sample=groups.get_group(selection['#CNAE'].loc[i]).sample(selection['samples'].loc[i])
    delivery=pd.concat((delivery,sample)).sort_values('Capital_Social',ascending=False)


print(delivery.columns)
print(delivery.Rua)
print(delivery.set_index('cnpj').columns)

delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Rua','Número','Complemento_endereço',
                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]

编辑2

新的奇怪的东西: 我放弃了,并从最后一段代码中删除了“Rua”,希望它能起作用。令我惊讶的是,我也遇到了同样的问题,但现在我的专栏是“Número”。在

delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Número','Complemento_endereço',
                                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica' ]]

KeyError: "['Número'] not in index"

编辑3

然后我放弃了“Número”并把它拿了出来。同样的问题也发生在“completo ou endereço”身上。然后我删除了“completoçu endereço”。它发生在“电传打字机”等等。在


Tags: inindexemailsocialprintdeliverycapitalrua
1条回答
网友
1楼 · 发布于 2024-04-29 14:46:51

发现问题出在“Bairro”而不是“Rua”中,但是pandas 0.18有一个bug,在它前面显示了这个项目。 奇怪的是,所有的星星都密谋反对。我以前试过升级Pandas,它把我升级到0.18版本,而不是0.20版本(有这个修复)。现在我把0.20改成了0.20。在

相关问题 更多 >