1

I'm working with what I think is a pandas multi-index data frame. Is there any way to make sure? My data looks like this.

            cinc           Outcome   
Side           1         2       1  2
WarNum                               
1       0.146344  0.029989       1  2
4       0.152565  0.056853       1  2
7       0.082757  0.017940       1  2
10      0.076032  0.022553       1  2
13      0.048538  0.005754       1  2

When I type in war_cinc.columns. I get the following output.

MultiIndex([(   'cinc', 1),
            (   'cinc', 2),
            ('Outcome', 1),
            ('Outcome', 2)],
           names=[None, 'Side'])

If I wanted to subset this data, how would I do so? (say I want to get the entire 2nd column of the cinc column of the dataframe)

1 Answer 1

1

To check if your df has Multiindex, you can do this:

isinstance(war_cinc.index, pd.MultiIndex)

This will return True.

To check for hierarchical columns, you can check nlevels:

if len(war_cinc.columns.nlevels) > 1:

This will be True in your case.

You can get the entire 2nd column like:

war_cinc[(   'cinc', 2)]

You need to pass all levels of the column in a tuple to fetch the column values.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.