简体   繁体   中英

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

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.

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

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

The following works but creates a new DataFrame :

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

Out[3]:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM