这是此问题答案的后续问题:
以下建议有效:
df = DataFrame(np.arange(20).reshape(5,4))
df2 = df.set_index(keys=[0,1,2])
df2.ix[(4,5,6)]
使用 MultiIndex
所以我创建了一个文件 sample_data.csv,如下所示:
col1,col2,year,amount
111111,3.5,2012,700
111112,3.5,2011,600
222221,4.0,2012,222
...
然后我运行了以下命令:
import numpy as np
import pandas as pd
sd=pd.read_csv('sample_data.csv')
sd2=sd.set_index(keys=['col2','year'])
sd2.ix[(4.0,2012)]
但这会产生以下错误:IndexError: index out of bounds
任何想法为什么它在前一种情况下有效,但在后一种情况下无效?这是错误的样子:
IndexError Traceback (most recent call last)
<ipython-input-19-1d72a961db95> in <module>()
----> 1 sd2.ix[(4.0,2012)]
/Library/Python/2.7/site-packages/pandas-0.8.1-py2.7-macosx-10.7-intel.egg/pandas/core/indexing.pyc in __getitem__(self, key)
31 pass
32
---> 33 return self._getitem_tuple(key)
34 else:
35 return self._getitem_axis(key, axis=0)