熊猫子集似乎无法根据python3.6中的索引起作用



我有一个奇怪的问题。我不知道它是否正确。我发现了这个问题的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错误的详细信息

需要 iloc

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

最新更新