Python Pandas通过二级索引(或任何其他级别)切片多索引

时间:2015-10-18 03:32:42

标签: python sorting pandas slice multi-index

在将多索引的级别[0]切割为级别1的范围时,有很多帖子。但是,我无法找到解决问题的方法;也就是说,我需要级别[0]索引值

的级别1索引范围

dataframe:首先是A到Z,Rank是1到400;我需要每个级别[0](第一个)的前2个和后2个,但不是在同一个步骤中。

           Title Score
First Rank 
A     1    foo   100
      2    bar   90
      3    lime  80
      4    lame  70
B     1    foo   400
      2    lime  300
      3    lame  200
      4    dime  100

我正在尝试使用以下代码获取每个级别1索引的最后两行,但它仅适用于第一级[0]值。

[IN]  df.ix[x.index.levels[1][-2]:]
[OUT] 
               Title Score
    First Rank 
    A     3    lime  80
          4    lame  70
    B     1    foo   400
          2    lime  300
          3    lame  200
          4    dime  100

我通过交换索引得到的前两行,但我不能让它在最后两行中运行。

df.index = df.index.swaplevel("Rank", "First")
df= df.sortlevel() #to sort by Rank
df.ix[1:2] #Produces the first 2 ranks with 2 level[1] (First) each.
           Title Score
Rank First 
1     A    foo   100
      B    foo   400
2     A    bar   90
      B    lime  300

当然我可以换回来得到这个:

df2 = df.ix[1:2]
df2.index = ttt.index.swaplevel("First","rank") #change the order of the indices back.
df2.sortlevel()
               Title Score
    First Rank 
    A     1    foo   100
          2    bar   90
    B     1    foo   400
          2    lime  300

任何帮助都表示赞赏使用相同的程序:

  • 索引1(排名)的最后两行
  • 获得前两行的更好方法

通过@ako编辑以下反馈:

使用pd.IndexSlice确实可以轻松切片任何级别索引。这是一个更通用的解决方案,下面是我逐步获取第一行和最后两行的方法。更多信息请访问:http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers

"""    
Slicing a dataframe at the level[2] index of the
major axis (row) for specific and at the level[1] index for columns.

"""
    df.loc[idx[:,:,['some label','another label']],idx[:,'yet another label']]

"""
Thanks to @ako below is my solution, including how I
get the top and last 2 rows.
"""
    idx = pd.IndexSlice
    # Top 2
    df.loc[idx[:,[1,2],:] #[1,2] is NOT a row index, it is the rank label. 
    # Last 2
    max = len(df.index.levels[df.index.names.index("rank")]) # unique rank labels
    last2=[x for x in range(max-2,max)]
    df.loc[idx[:,last2],:] #for last 2 - assuming all level[0] have the same lengths.

2 个答案:

答案 0 :(得分:13)

使用索引器切割任意维度的任意值 - 只需传递一个列表,其中包含该维度所需的任何级别/值。

animateWithDuration

用于复制数据:

idx = pd.IndexSlice
df.loc[idx[:,[3,4]],:]

           Title  Score
First Rank             
A     3     lime     80
      4     lame     70
B     3     lame    200
      4     dime    100

答案 1 :(得分:5)

在多级索引中按第二(子)级切片的另一种方法是将slice(None).loc[]一起使用。对级别使用slice(None)表示未对特定索引进行切片,然后为要切片的索引传递单个项目或列表。希望对以后的读者有帮助

df.loc[ ( slice(None), [3, 4] ),  : ]