2014-09-22 67 views
0

我有一個多索引DF結構如下:排名多索引DF

>>> df = pd.DataFrame({(2014, 'value'): {('AR', 0): 1.2420, ('AR', 1): 0.1802,('BR', 0): 1.3,('BR', 1): 0.18}}) 
>>> print df 

     2014 
     value 
AR 0 1.2420 
    1 0.1802 
BR 0 1.3000 
    1 0.1800 

我的目標是增加一列「等級」,即包含的國家排名(AR & BR )爲0 & 1以降序排列。期望的結果會是這樣的:

  2014   
      value rank 
iso id 
AR 0  1.2420 2  
     1  0.1802 1  
BR 0  1.3  1  
     1  0.18 2 

我最初的做法是重置索引:

>>> df = df.reset_index() 
>>> print df 

     level_0 level_1 2014 
          value 
0  AR  0   1.2420 
1  AR  1   0.1802 
2  BR  0   1.3000 
3  BR  1   0.1800 

,然後使用GROUPBY和等級添加「排名」列:

>>> df[2014, 'gr'] = df.groupby(['level_1'])[2014, 'value'].rank(ascending=False) 

但是,該結果在:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 2990, in __getitem__ 
    if len(self.obj.columns.intersection(key)) != len(key): 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 3774, in intersection 
    result_names = self.names if self.names == other.names else None 
AttributeError: 'tuple' object has no attribute 'names' 

我在正確的軌道上,我應該考慮另一種方法嗎?

回答

2

所以排名來自value對不對?我認爲這是你想要什麼:

In [13]: df.groupby(level=1).rank(ascending=False) 
Out[13]: 
     2014 
    value 
AR 0  2 
    1  1 
BR 0  1 
    1  2 

,你可以用df['rank'] = df.groupby(level=1).rank(ascending=False)

+0

感謝設置。這比我想象的更簡單。我設置的值略有不同:'''df [(2014,'rank')] = df.groupby(level = 1).rank(ascending = False)'''' – Olaf 2014-09-23 06:18:13