Finding a column within Multi-index

Question:

Ho would I refer to a column of Price and Small as Example from Code Below
`

dx = pd.MultiIndex.from_product([['Quantity', 'Price'], ['medium', 'large', 'small']])

idx
MultiIndex([('Quantity', 'medium'),
            ('Quantity',  'large'),
            ('Quantity',  'small'),
            (   'Price', 'medium'),
            (   'Price',  'large'),
            (   'Price',  'small')],
           )

df[idx]

`

I tried df(‘Price’,’small’) but honestly a bit new at this and not sure how to refer

Asked By: elir1

||

Answers:

When you have a single-level / flat index, the column coordinate is a simple string:

df["ColumnName"]

When your dataframe columns is a multi-index, the coordinate is an n-tuple:

df[("NameAtLevel0", "NameAtLevel1", "NameAtLevel2")]

Follow that pattern, to retrieve your Price-Small column:

df[("Price", "small")]
Answered By: Code Different
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.