concise way of flattening multiindex columns

You can do a map join with columns

out.columns = out.columns.map('_'.join)
out
Out[23]: 
     B_mean     B_std  C_median
A                              
1  0.204825  0.169408  0.926347
2  0.362184  0.404272  0.224119
3  0.533502  0.380614  0.218105

For some reason (when the column contain int) I like this way better

out.columns.map('{0[0]}_{0[1]}'.format) 
Out[27]: Index(['B_mean', 'B_std', 'C_median'], dtype="object")

Leave a Comment