在Pandas中排序后保留索引

时间:2017-01-19 18:15:43

标签: python pandas jupyter

我最近开始在Jupyter Notebook中使用Pandas(Python 3.5)。我设置了一个索引然后排序,这是第一次工作。但是当我再次尝试排序时,我收到一条错误消息,我的索引变量无法识别。

读入档案

raw_df = pd.ExcelFile('MyFile',parse_dates=True).parse('Raw Data')

设置索引

raw_df = raw_df.set_index('OrderId')
raw_df = raw_df.sort_values(by='WeekOrdered')
raw_df.head()

正如我上面所说,当我第一次运行单元格时,它运行正常。但是当我再次尝试运行时,我收到了错误消息。

C:\Users\...\Anaconda3\lib\site-packages\pandas\indexes\base.py
in get_loc(self, key, method, tolerance)    1944             try:
-> 1945                 return self._engine.get_loc(key)    1946             except KeyError:

pandas\index.pyx in pandas.index.IndexEngine.get_loc
(pandas\index.c:4154)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc
(pandas\index.c:4018)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(pandas\hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(pandas\hashtable.c:12322)()

KeyError: 'OrderId'

为什么我不能再次运行这个单元格的任何想法?

0 个答案:

没有答案
相关问题