Reputation: 5637
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.
Upvotes: 8
Views: 8884
Reputation: 294258
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
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
Upvotes: 22
Reputation: 862641
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
Upvotes: 4
Reputation: 5637
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
Upvotes: 0