在Pandas数据框中访问行的正确语法

时间:2018-06-25 14:07:29

标签: python-3.x pandas dataframe

我从熊猫开始,但是已经在非常基本的功能上苦苦挣扎,根据过程,结果应该返回数据帧的第二行,但是返回错误。此数据框查询有什么问题?

>>> import pandas as pd
>>> df = pd.read_csv("../data/gapminder.tsv", sep="\t")
>>> print(df.head())
       country continent  year  lifeExp       pop   gdpPercap
0  Afghanistan      Asia  1952   28.801   8425333  779.445314
1  Afghanistan      Asia  1957   30.332   9240934  820.853030
2  Afghanistan      Asia  1962   31.997  10267083  853.100710
3  Afghanistan      Asia  1967   34.020  11537966  836.197138
4  Afghanistan      Asia  1972   36.088  13079460  739.981106
>>> df [[1]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", 
line 2133, in __getitem__
    return self._getitem_array(key)
  File "/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2177, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", 
line 1269, in _convert_to_indexer
    .format(mask=objarr[mask]))
KeyError: '[1] not in index'
>>>

df命令,例如“国家”效果很好

0 个答案:

没有答案