1

I'm starting with pandas but already struggle with very basic function that according to the course should return as a result the second row of a dataframe, but returns an error. What is wrong in this dataframe query?

>>> 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'
>>>

A df command on e.g. "country" works fine

Steffen.H
  • 11
  • 1

0 Answers0