Pivot with Categories

PHOTO EMBED

Sat May 07 2022 20:16:52 GMT+0000 (Coordinated Universal Time)

Saved by @vivienfeng11

tran_data['charge_type']=pd.Categorical(tran_data['charge_type'],categories = ["Usage_gross","Mileage_gross","Late_return_gross","PCN_gross","PCN_admin_gross","Flex_gross","Driving_credits_gross","Total_VAT"],ordered=True)
df_pivot = tran_data.pivot_table(index=['reservation_id'],values=["amount"],columns=['charge_type'],aggfunc=np.sum).fillna(0)

df_pivot.columns = df_pivot.columns.droplevel(0)

flattened = pd.DataFrame(df_pivot.to_records()) # convert pivot table to dataframe
content_copyCOPY

http://localhost:8888/notebooks/Z4B invoice - Croydon 22-02-09.ipynb