1

I have a multiindex dataframe:

  unique     cat     numerical    
       c   f   b   d         a   e
0      2   5   1   3         0   4
1      8  11   7   9         6  10
2     14  17  13  15        12  16

I'm trying to get the column names under 'unique' and 'cat'. For just 'unique' alone, I did:

print(df.loc[:,'unique'].columns)

yielding:

Index(['c', 'f'], dtype='object')

Evidently, that works. Then, I tried to it for 2 columns at once:

new_df = df.loc[:,['cat','unique']].swaplevel(0,1,1)
print(new_df)

yields:

       c      f   b   d
  unique unique cat cat
0      2      5   1   3
1      8     11   7   9
2     14     17  13  15

So far, so good. But when I try:

print(new_df.columns)

I get:

MultiIndex(levels=[['a', 'b', 'c', 'd', 'e', 'f'], ['cat', 'numerical', 'unique']],
           labels=[[2, 5, 1, 3], [2, 2, 0, 0]])

Which is the same as when printing the columns of the original dataframe. What's going on?

Nathan Tew
  • 432
  • 1
  • 5
  • 21
  • 1
    You are looking at a view of the previous dataframe `df`. See here for a good explanation: https://stackoverflow.com/a/23296545/8146556 – rahlf23 Sep 24 '18 at 15:38

0 Answers0