Pandas 根据Python3.6中的索引,熊猫子集似乎不起作用

Pandas 根据Python3.6中的索引,熊猫子集似乎不起作用,pandas,Pandas,我有个奇怪的问题。我不知道它是否正确。我在python3.6中发现了这个问题 单击以获取数据集 df = pd.read_csv("./data/gapminder.tsv",sep="\t") 下面的代码不会产生任何错误 subset = df[['country', 'pop']] subset.head() 但如果我试图根据索引进行子集划分,我就会出错 subset = df[[0,4]] > KeyError: '[0 4] not in index' 请在需求中找到ipyt

我有个奇怪的问题。我不知道它是否正确。我在python3.6中发现了这个问题

单击以获取数据集

df = pd.read_csv("./data/gapminder.tsv",sep="\t")
下面的代码不会产生任何错误

subset = df[['country', 'pop']]
subset.head()
但如果我试图根据索引进行子集划分,我就会出错

subset = df[[0,4]]
> KeyError: '[0 4] not in index'
请在需求中找到ipython错误的详细信息:

url = 'https://raw.githubusercontent.com/jennybc/gapminder/master/inst/gapminder.tsv'
df = pd.read_csv(url, 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

subset = df[['country', 'pop']]
print (subset.head())
       country       pop
0  Afghanistan   8425333
1  Afghanistan   9240934
2  Afghanistan  10267083
3  Afghanistan  11537966
4  Afghanistan  13079460

subset = df.iloc[:, [0,4]]
print (subset.head())
       country       pop
0  Afghanistan   8425333
1  Afghanistan   9240934
2  Afghanistan  10267083
3  Afghanistan  11537966
4  Afghanistan  13079460