Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I flatten a hierarchical column index in a pandas DataFrame?

Tags:

python

pandas

Say I have a pandas.DataFrame with a hierarchical index on 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

Out[1]:

   A     B   
   a  b  a  b
0  0  1  2  3
1  4  5  6  7

I would like to flatten the column index so it looks as follows:

   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 that just ignored the modified name of the new columns.

like image 435
snth Avatar asked Jan 30 '17 08:01

snth


2 Answers

set_axis

df.set_axis([f"{x}{y}" for x, y in df.columns], axis=1, inplace=False)

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

index.map

df.columns = df.columns.map(''.join)
df

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

For non-string column values

df.columns = df.columns.map(lambda x: ''.join([*map(str, x)]))
df

   Aa  Ab  Ba  Bb
0   0   1   2   3
1   4   5   6   7
like image 117
piRSquared Avatar answered Oct 20 '22 21:10

piRSquared


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
like image 23
jezrael Avatar answered Oct 20 '22 20:10

jezrael