How to smooth the index of a hierarchical column in a pandas DataFrame?

Let's say that I have a pandas.DataFrame with a hierarchical index in the columns as follows:

 import pandas as pd columns = pd.MultiIndex.from_product([list('AB'), list('ab')]) df = pd.DataFrame(np.arange(8).reshape((2,4)), columns=columns) print df 

From [1]:

  AB abab 0 0 1 2 3 1 4 5 6 7 

I would like to flatten the column index so that it looks like this:

  Aa Ab Ba Bb 0 0 1 2 3 1 4 5 6 7 

I tried

 def flatten(col): col.name = ''.join(col.name) return col df.apply(f) 

but it just ignored the changed name of the new columns.

+5
source share
3 answers

use map

 df.columns = df.columns.map(''.join) df Aa Ab Ba Bb 0 0 1 2 3 1 4 5 6 7 

use rename

 df.rename(columns=''.join) Aa Ab Ba Bb 0 0 1 2 3 1 4 5 6 7 
+10
source

You can use list comprehension with join :

 df.columns = [''.join(col) for col in df.columns] print (df) Aa Ab Ba Bb 0 0 1 2 3 1 4 5 6 7 

Another possible solution:

 df.columns = df.columns.to_series().str.join('') print (df) Aa Ab Ba Bb 0 0 1 2 3 1 4 5 6 7 
+3
source

The following works, but creates a new DataFrame :

 df_flat = pd.DataFrame({''.join(k):v for k,v in df.iteritems()}) print df_flat 

From [3]:

  Aa Ab Ba Bb 0 0 1 2 3 1 4 5 6 7 
0
source

Source: https://habr.com/ru/post/1263563/


All Articles