使用索引访问dataframe列

时间:2017-09-12 17:13:03

标签: python pandas dataframe indexing

我的数据框有A,B,C作为列。我已经使用

设置了一个索引
df.set_index('A')

现在我想使用像

这样的条件语句来过滤行
df[df.loc['A'] == '10001']

这给了我以下错误:

KeyError: 'the label [A] is not in the [index]'

如何在我设置为索引的列上应用条件?

2 个答案:

答案 0 :(得分:0)

您必须指定'inplace = True'

df.set_index('A', inplace=True)

否则它不会持续存在。

答案 1 :(得分:0)

考虑这个df

        val
A   
10001   5
10002   6
10003   3

您可以使用

过滤行
df[df.index == '10001'] 

你得到了

        val
A   
10001   5