Home > database >  Finding a column within Multi-index
Finding a column within Multi-index

Time:11-22

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

CodePudding user response:

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")]
  • Related