I have a Pandas df with MultiIndex column-labels like this:
in:
import pandas as pd
import numpy as np
np.random.seed(123)
df = pd.DataFrame(np.random.randint(100,size=(3, 4)),columns = pd.MultiIndex.from_product([['exp0','exp1'],['rnd0','rnd1']],names=['experiments','rnd_runs']))
out:
experiments exp0 exp1
rnd_runs rnd0 rnd1 rnd0 rnd1
0 66 92 98 17
1 83 57 86 97
2 96 47 73 32
I would like to have multiple quantile computations (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.quantile.html).
I can do it for a single quantile calculation:
in:
df.groupby(axis='columns',level='experiments').quantile(q=0.1)
out:
experiments exp0 exp1
0 68.6 25.1
1 59.6 87.1
2 51.9 36.1
But using a list of q's upsets Pandas:
in:
df.groupby(axis='columns',level='experiments').quantile(q=[0.1,0.9])
out:
ValueError Traceback (most recent call last)
<ipython-input-54-86a5c92468f5> in <module>
----> 1 df.groupby(axis='columns',level='experiments').quantile(q=[0.1,0.9])
~\AppData\Local\Continuum\anaconda3\envs\suite2p\lib\site-packages\pandas\core\groupby\groupby.py in quantile(self, q, interpolation)
1949
1950 # reorder rows to keep things sorted
-> 1951 indices = np.arange(len(result)).reshape([len(q), self.ngroups]).T.flatten()
1952 return result.take(indices)
1953
ValueError: cannot reshape array of size 6 into shape (2,2)
Can you explain why and help me with the right syntax?