Here's my pivot table column structure (multiindex):
col2 col3 col4 sales
month month_1 month_2 month_3
I would like to flatten it to:
col2 col3 col4 month_1 month_2 month_3
If I do pivot.columns = pivot.columns.get_level_values(0), then the result is:
col2 col3 col4 sales sales sales
What do I do?
I think solution is remove [] around [sales] and [months] if pivoting only by one column sales.
So code is:
pivot = (pd.pivot_table(df,
index=['col2','col3','col4'],
columns='month',
values='sales')
.reset_index()
.rename_axis(None, axis=1))
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