I have used pd.pivot_table
in pandas dataframe, and the columns names becomes tuples like (\'A1\', \'B1\'), (\'A1\', \'B2\')...
and I want them to
setup
df = pd.DataFrame(
np.arange(8).reshape(2, 4),
columns=[('A1', 'B1'), ('A2', 'B1'), ('A1', 'B2'), ('A2', 'B2')])
print(df)
(A1, B1) (A2, B1) (A1, B2) (A2, B2)
0 0 1 2 3
1 4 5 6 7
rename
df.rename(columns='_'.join, inplace=True)
print(df)
A1_B1 A2_B1 A1_B2 A2_B2
0 0 1 2 3
1 4 5 6 7
map
df.columns = df.columns.map('_'.join)
print(df)
A1_B1 A2_B1 A1_B2 A2_B2
0 0 1 2 3
1 4 5 6 7