Pandas Dataframe索引:KeyError:[columns]中没有[columns]

时间:2016-08-31 08:27:54

标签: python python-3.x pandas

我正在使用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()选择很好。错误消息来自熊猫,所以我重新安装了pip3;它没有帮助,apt-get更新升级等,没有变化。

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

0 个答案:

没有答案