ziulfer
ziulfer

Reputation: 1369

Access pandas dataframe column with two header pandas

I created a dataframe using groupby and pd.cut to calculate the mean, std and number of elements inside a bin. I used the agg()and this is the command I used:

df_bin=df.groupby(pd.cut(df.In_X, ranges,include_lowest=True)).agg(['mean', 'std','size'])

df_bin looks like this:

                 X                  Y
                 mean   std size   mean         std  size
In_X                    
(10.424, 10.43] 10.425  NaN  1      0.003786    NaN   1
(10.43, 10.435] 10.4    NaN  0      NaN         NaN   0

I want to create an array with the values of the mean for the first header X. If I didn't have the two header level, I would use something like:

mean=np.array(df_bin['mean'])

But how to do that with the two headers?

Upvotes: 0

Views: 1184

Answers (2)

r.ook
r.ook

Reputation: 13868

This documentation would serve you well: https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html

To answer your question, if you just want a particular column:

mean = np.array(df_bin['X', 'mean'])

But if you wanted to slice to the second level:

mean = np.array(df_bin.loc[:, (slice(None), 'mean')])

Or:

mean = np.array(df_bin.loc[:, pd.IndexSlice[:, 'mean']])

Upvotes: 2

BENY
BENY

Reputation: 323226

We can do

df_bin.stack(level=0)['mean'].values

Upvotes: 1

Related Questions