Reputation: 429
I have a MultiIndex dataframe with 200 columns. I would like to select a specific column from that. Suppose df is some part of my dataframe:
df=
a b
l h l h l h l
cold hot hot cold cold hot hot
2009-01-01 01:00:00 0.1 0.9 0.4 0.29 0.15 0.6 0.3
2009-01-01 02:00:00 0.1 0.8 0.35 0.2 0.15 0.6 0.4
2009-01-01 03:00:00 0.12 0.7 0.3 0.23 0.23 0.8 0.3
2009-01-01 04:00:00 0.1 0.9 0.33 0.24 0.15 0.6 0.4
2009-01-01 05:00:00 0.17 0.9 0.41 0.23 0.18 0.75 0.4
I would like to select the values for this column[h,hot].
My output should be:
df['h','hot']=
a b
2009-01-01 01:00:00 0.9 0.6
2009-01-01 02:00:00 0.8 0.6
2009-01-01 03:00:00 0.7 0.8
2009-01-01 04:00:00 0.9 0.6
2009-01-01 05:00:00 0.9 0.75
I would appreciate any guidance on how I could select that.
Upvotes: 1
Views: 396
Reputation: 394389
For multi-index slicing as you desire the columns needs to be sorted first using sort_index(axis=1)
, you can then select the cols of interest without error:
In [12]:
df = df.sort_index(axis=1)
df['a','h','hot']
Out[12]:
0
2009-01-01 01:00:00 0.9
2009-01-01 02:00:00 0.8
2009-01-01 03:00:00 0.7
2009-01-01 04:00:00 0.9
2009-01-01 05:00:00 0.9
Name: (a, h, hot), dtype: float64
Upvotes: 1
Reputation: 46
Try this:
dataframe= pd.DataFrame()
dataframe["temp"] = df["b"]["h"]["hot"]
df - is your dataframe
Upvotes: 0