0

if I have a multi-index data frame in pandas like this.

test.index.names
FrozenList(['target', 'h', 'd', 'y', 'obsrv'])

test.index.values
('A', '15', '60', '0', 97),
('B', '15', '60', '0', 98),

I can index the data frame associated with target = A, h = 15, d = 60, y = 0, obrv = 97 by using a tuple like this

test.loc[('A', '15', '60', '0', 97)]

But I want to index this data frame somehow like this.

test(target = 'A', h = 15)...

such that the order inside does not matter. Is that possible ?

jav321
  • 131
  • 1
  • 11

2 Answers2

0

Instead of loc you can use .xs() to get cross sections with the index intact. Not exactly the form you're looking for, but should serve your purposes I'm guessing:

test.xs(('A', '15', '60', '0', 97), level=('target', 'h', 'd', 'y', 'obsrv'))

You could then use the answer provided in DataFrame with MultiIndex to dict to convert to a dict.

I can give a more specific answer for your problem if you can provide a dataset.

ZaxR
  • 4,269
  • 3
  • 18
  • 39
0

For multiple index

test.loc[pd.IndexSlice['A', '15',:,:,:],:]
BENY
  • 296,997
  • 19
  • 147
  • 204