Struggling mightily with formatting a pandas pivot. I have screenshot what I'm getting with the existing code, and where I Hope to take this.
Simply put my goals are to:
1 - Sort the Column field "animal_name" in an ascending order (so each animal_name will have a column for every "value" listed in the pivot code below.
2 - Swap Rows 1 & 2 (literally flipping them so row 2 becomes row 1 and row 1 becomes row 2.
Please note, the field that populates JRE and MXE is anm_name in the code below
Here's the code I'm using:
pg = pd.pivottable(df, values = ['anm_pct_mv','port_pct_avg'],index=['asof_dt',"Code","Animal Breakout 6","Animal Breakout 7"], columns = 'anm name').reset_index()
pg.to_csv (r'\desktop', index = False, header =True)
Raw Data, how it is after code, and how I want it to be displayed:
WHAT Data Currently Is
anm_pct_avg anm_pct_avg anm_pct_mv anm_pct_mv
asof_dt Code Animal Breakout 6 Animal Breakout 7 JRE MXE JRE MXE
09/02/2022 JZR-Juru JZR Juru 134 234 234232 134223
09/02/2022 JZL-Jaja JZL Jaja 1343 1234 1343421 1214313
WHAT I WANT IT TO BE
asof_dt Code Animal Breakout 6 Animal Breakout 7 JRE JRE MXE MXE
asof_dt Code Animal Breakout 6 Animal Breakout 7 anm_pct_avg anm_pct_mv anm_pct_avg anm_pct_avg
09/02/2022 JZR-Juru JZR Juru 134 234 234232 134223
09/02/2022 JZL-Jaja JZL Jaja 1343 1234 1343421 1214313