无法设置 pandas 数据帧的索引 - 获取“KeyError";

2022-01-17 00:00:00 python pandas dataframe set row

问题描述

我生成了一个如下所示的数据框(summaryDF):

I generate a data frame that looks like this (summaryDF):

   accuracy        f1  precision    recall
0     0.494  0.722433   0.722433  0.722433
0     0.290  0.826087   0.826087  0.826087
0     0.274  0.629630   0.629630  0.629630
0     0.278  0.628571   0.628571  0.628571
0     0.288  0.718750   0.718750  0.718750
0     0.740  0.740000   0.740000  0.740000
0     0.698  0.765133   0.765133  0.765133
0     0.582  0.778547   0.778547  0.778547
0     0.682  0.748235   0.748235  0.748235
0     0.574  0.767918   0.767918  0.767918
0     0.398  0.711656   0.711656  0.711656
0     0.530  0.780083   0.780083  0.780083

因为我知道其中的每一行应该是什么,所以我使用这段代码来设置每一行的名称(这些不是实际的行名,只是为了论证).

Because I know what each row in this should be, I then am using this code to set the names of each row (these aren't the actual row names but just for argument's sake).

summaryDF = summaryDF.set_index(['A','B','C', 'D','E','F','G','H','I','J','K','L'])

但是,我得到:

level = frame[col].values
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1797, in __getitem__
    return self._getitem_column(key)
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1804, in _getitem_column
    return self._get_item_cache(key)
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 1084, in _get_item_cache
    values = self._data.get(item)
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 2851, in get
    loc = self.items.get_loc(item)
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1572, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)
  File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)
  File "pandas/hashtable.pyx", line 686, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12280)
  File "pandas/hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231)
KeyError: 'A'

我不知道自己做错了什么,并且进行了广泛的研究.有什么想法吗?

I have no idea what I am doing wrong and have researched far and wide. Any ideas?


解决方案

我猜你和@jezrael 误解了 pandas 文档中的一个例子:

I guess you and @jezrael misunderstood an example from the pandas docs:

df.set_index(['A', 'B'])

AB 在本例中是列名/标签:

A and B are column names / labels in this example:

In [55]: df = pd.DataFrame(np.random.randint(0, 10, (5,4)), columns=list('ABCD'))

In [56]: df
Out[56]:
   A  B  C  D
0  6  9  7  4
1  5  1  3  4
2  4  4  0  5
3  9  0  9  8
4  6  4  5  7

In [57]: df.set_index(['A','B'])
Out[57]:
     C  D
A B
6 9  7  4
5 1  3  4
4 4  0  5
9 0  9  8
6 4  5  7

文档说应该是列表列标签/数组.

The documentation says it should be list of column labels / arrays.

所以你正在寻找:

In [58]: df.set_index([['A','B','C','D','E']])
Out[58]:
   A  B  C  D
A  6  9  7  4
B  5  1  3  4
C  4  4  0  5
D  9  0  9  8
E  6  4  5  7

但正如@jezrael 所建议的那样, df.index = ['A','B',...] 是更快、更惯用的方法...

but as @jezrael has suggested df.index = ['A','B',...] is faster and more idiomatic method...

相关文章