I have a pandas dataframe df
that I built using 3 levels of columns, as follows:
a1 a2 a3
b1 b2 b1 b3 b1 b4
c1 c2 c1 c2 c1 c2 c1 c2 c1 c2 c1 c2
... (data) ...
Note that each a
column may have different b
subcolumns, but each b
column has the same c
subcolumns.
I can extract e.g. the subcolumns from a2
using df["a2"]
.
How can I select based on the second or third level without having to specify the first and second level respectively? For instance I would like to say "give me all the c2
columns you can find" and I would get:
a1 a2 a3
b1 b2 b1 b3 b1 b4
... (data for the c2 columns) ...
Or "give me all the b1
columns" and I would get:
a1 a2 a3
c1 c2 c1 c2 c1 c2
... (data for the b1 columns) ...