Python Pandas fragment multiindex index of the second level (or any other level)

There are many reports about the cut of level [0] of a multi-index into a range of level 1 . However, I cannot find a solution to my problem; that is, I need a level 1 range index for index values ​​[0]

dataframe: first from A to Z, Rank - from 1 to 400; I need the first 2 and last 2 for each level [0] (first), but not at the same step.

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 

I am trying to get the last 2 lines for each level 1 with the code below, but it only cuts correctly for the first level [0] value.

 [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 

The first 2 lines I get by replacing the indices, but I can't get it to work for the last two lines.

 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 

Of course, I can change this to get the following:

 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 

Any help would be appreciated with the same procedure:

  • Last 2 rows for index 1 (Rank)
  • And the best way to get the first 2 lines

Edit the following feedback from @ako:

Using pd.IndexSlice really makes it easy to crop any level index. Here is a more general solution and below my phased approach to get the first and last two lines. More information here: 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. 
+7
python sorting pandas slice multi-index
source share
1 answer

Using an indexer to slice arbitrary values ​​into arbitrary sizes - just pass a list with any desired levels / values ​​for this dimension.

 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 

To play data:

 from StringIO import StringIO s=""" First Rank Title Score A 1 foo 100 A 2 bar 90 A 3 lime 80 A 4 lame 70 B 1 foo 400 B 2 lime 300 B 3 lame 200 B 4 dime 100 """ df = pd.read_csv(StringIO(s), sep='\s+', index_col=["First", "Rank"]) 
+10
source share

All Articles