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
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter