Question

Say I have a sorted Multi-Index Dataframe:

color  food      one        two
green  ham   0.565738  1.545659
       eggs -0.974236 -0.070345
red    eggs  1.033801 -2.400454
       ham  -0.785435  0.462060
       pork  0.704228  0.523508

...

How can I retrieve all elements associated with the Kth value for the 1st level. For example, if K=2, we would retrieve:

red    eggs  1.033801 -2.400454
       ham  -0.785435  0.462060
       pork  0.704228  0.523508

More generally, what is the syntax for iloc for Multi-level DataFrames?

Update:

See this ticket on GitHub for a follow-up on the topic: "Awkward" Integer-indexing in MultiIndex DataFrames

Was it helpful?

Solution

Assuming that you are looking for all rows associated with the Kth value on your Nth level:

df.xs(df.index.levels[N][K], level=N)

OTHER TIPS

The indexing docs are pretty complete, it pays to read and experiment:

Select by label

In [18]: df.loc[['green']]
Out[18]: 
                 one       two
green ham   0.261355  0.182691
      eggs  0.243253 -0.360223

[2 rows x 2 columns]

You can directly index with iloc if you want

In [24]: df.iloc[0:2]
Out[24]: 
                 one       two
green ham   0.261355  0.182691
      eggs  0.243253 -0.360223

[2 rows x 2 columns]

This will give you at most the kth (in this case 2) values for each of the values on level=0

In [35]: df.groupby(level=0).head(2)
Out[35]: 
                 one       two
green ham   0.261355  0.182691
      eggs  0.243253 -0.360223
red   eggs -0.147635  0.555402
      ham   1.815182  0.158497

[4 rows x 2 columns]

Here is the nth value (k-1); if a group doesn't have one nothing will be returned for that key

In [36]: df.groupby(level=0).nth(2)
Out[36]: 
               one       two
red pork -0.158261 -0.963434

[1 rows x 2 columns]

In [37]: df.groupby(level=0).nth(1)
Out[37]: 
                 one       two
green eggs  0.243253 -0.360223
red   ham   1.815182  0.158497

[2 rows x 2 columns]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top