从列设置索引时熊猫键错误

时间:2018-07-04 10:18:22

标签: python pandas dataframe

我有这个熊猫数据框:

   Parameter  Coordinate  Velocity
0  quantity   56.943481  0.000634
1   b_start    0.715886  0.000634
2     b_end    0.221384  0.000634
3   q_short   54.906629  0.000634
4   b_price    0.545564  0.000634
5        t1    0.622845  0.000634
6        t2    0.480350  0.000634
7        t3    0.212344  0.000634
8        t4    0.059454  0.000634
9        t5    0.636290  0.000634

我想通过使用“参数”作为索引来访问其中的元素,所以我这样做:

self.p.set_index('Parameter', inplace=True)
print(self.p)

到目前为止,我很好:

           Coordinate  Velocity
Parameter                      
quantity    56.943481  0.000634
b_start      0.715886  0.000634
b_end        0.221384  0.000634
q_short     54.906629  0.000634
b_price      0.545564  0.000634
t1           0.622845  0.000634
t2           0.480350  0.000634
t3           0.212344  0.000634
t4           0.059454  0.000634
t5           0.636290  0.000634

因此,如果我没记错的话,从现在开始,我的“参数”列就是我的索引。但是,当我尝试使用此新索引访问行时,例如:

print(self.p['quantity'])

我收到一个关键错误:

Traceback (most recent call last):
  File "/anaconda3/envs/dc/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3064, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'quantity'

有人知道我在做什么错吗?

0 个答案:

没有答案
相关问题