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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With