Bishonen_PL
Bishonen_PL

Reputation: 1571

Multiindexed columns - Select inner

having a DataFrame like the following:

frame = pd.DataFrame(np.arange(12).reshape((4, 3)),
   ....:                      index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],
   ....:                      columns=[['Ohio', 'Ohio', 'Colorado'],
   ....:                               ['Green', 'Red', 'Green']])

Whats the simplest way to select all columns with the color Green?

frame['Green']

... does not work.

Upvotes: 1

Views: 31

Answers (2)

piRSquared
piRSquared

Reputation: 294516

xs

This method was designed for this purpose.

frame.xs('Green', axis=1, level=1)
# frame.xs('Green', 1, 1)

     Ohio  Colorado
a 1     0         2
  2     3         5
b 1     6         8
  2     9        11

Or keep the level

frame.xs('Green', axis=1, level=1, drop_level=False)

     Ohio Colorado
    Green    Green
a 1     0        2
  2     3        5
b 1     6        8
  2     9       11

This is roughly equivalent to:
Obviously more complicated but interesting to see.

frame.loc[:, frame.columns.get_level_values(1) == 'Green']

Or

frame.loc[:, frame.columns.labels[1] == frame.columns.levels[1].get_loc('Green')]

swaplevel

I don't like this way but good for informative purposes

frame.swaplevel(0, 1, 1).Green

     Ohio  Colorado
a 1     0         2
  2     3         5
b 1     6         8
  2     9        11

Upvotes: 2

BENY
BENY

Reputation: 323376

This is multiple index , You can using IndexSlice

frame.loc[:,pd.IndexSlice[:,'Green']]
Out[506]: 
     Ohio Colorado
    Green    Green
a 1     0        2
  2     3        5
b 1     6        8
  2     9       11

Upvotes: 2

Related Questions