-2

I am looking for an efficient way to query a Multiindex Dataframe.

The index is (date,label)

For example how can I query all entries of a specific label

Here is the structure ---> image Link

  • 1
    It is helpful to add a minimal example of the dataframe would like to work on. – tnknepp Jun 08 '18 at 10:45
  • There is a linked image in the question. The dataframe comes from an Framework API Call So I can not post any textual structure easily – user7765301 Jun 08 '18 at 10:48
  • Welcome to SO. Please provide a **[mcve]**. This means data as text, no images / links. See **[How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)** for advice. – jpp Jun 08 '18 at 10:48
  • Can you add to answer `print (df.head().to_dict())` ? Because it seems there is no `MultiIndex` – jezrael Jun 08 '18 at 10:50
  • @jezrael type(df.index) results is -> and print (df.head().to_dict()) is None – user7765301 Jun 08 '18 at 10:54
  • Hmmm, it is weird, `print (df.head().to_dict())` should return `dictionary of DataFrame` – jezrael Jun 08 '18 at 10:58

1 Answers1

0

You can take a cross section of your data using "xs", you must also specify the index level (for you, that is probably level=1), and they key. See below.

import pandas as pd
import numpy as np

# build the milti-indexed dataframe
index1 =  list('ABC')
index2 = np.arange(5)
mli = pd.MultiIndex.from_product([index1, index2])
df = pd.DataFrame(index=mli, columns=list('xyz'), data = np.random.randint(0,10,(15,3))

>>> df
    Out[9]:
    ...:      x  y  z
    ...: A 0  7  9  1
    ...:   1  9  0  9
    ...:   2  2  0  7
    ...:   3  5  2  5
    ...:   4  3  4  5
    ...: B 0  3  9  9
    ...:   1  7  0  2
    ...:   2  6  9  2
    ...:   3  8  1  0
    ...:   4  7  7  5
    ...: C 0  9  1  1
    ...:   1  7  5  7
    ...:   2  5  6  9
    ...:   3  5  0  1
    ...:   4  3  4  0

# slice out all values in column "x" that have an index value=='A' in the first level of the index (i.e. level=0)
>>>df.xs(level=0, key='A').x
    ...: Out[10]:
    ...: 0    7
    ...: 1    9
    ...: 2    2
    ...: 3    5
    ...: 4    3
    ...: Name: x, dtype: int64
    ...:
tnknepp
  • 5,888
  • 6
  • 43
  • 57