gpt4 book ai didi

python - Pandas 数据帧索引 : KeyError:none of [columns] are in the [columns]

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

我在 datacamp 的“数据科学中级 Python”。

>>> brics.loc[:]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
>>> brics.loc[:,['country','capital']]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 1294, in __getitem__
return self._getitem_tuple(key)
File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 789, in _getitem_tuple
self._has_valid_tuple(tup)
File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 142, in _has_valid_tuple
if not self._has_valid_type(k, i):
File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 1379, in _has_valid_type
(key, self.obj._get_axis_name(axis)))
KeyError: "None of [['country', 'capital']] are in the [columns]"

这是教科书索引,昨晚有效,但今天无效。
>>> brics.iloc[1,1]
' Moscow'
>>> brics.iloc[1,2]
17.100000000000001

就像我唯一被破坏的函数是 loc(); iloc() 选择得很好。错误消息来自 Pandas ,所以我用 pip3 重新安装;它没有帮助,没有 apt-get update upgrade 等,没有变化。

看起来由于某种原因,pandas 没有在 loc 方法中正确解析我的字符串。

最佳答案

在这里,我使用 .loc 编写了简单的代码你可以看到下面的代码

import pandas as pd
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],'num_wings': [2, 0, 0, 0],'num_specimen_seen': [10, 2, 1, 8]},index=['falcon', 'dog', 'spider', 'fish'])
print(df.loc[:, 'num_legs':'num_wings'])
它在我的情况下有效,你可以试试这个

关于python - Pandas 数据帧索引 : KeyError:none of [columns] are in the [columns],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244466/

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