1

After doing some research I have found out that I cannot simply do subtotals after each row group in Pandas (which is default feature of Excel's pivot table - below an example)

Excel

Pandas pivot does it only as total for all groups, not for each group, as after every rectangle like below

Pivot

My code :

ramka = pd.DataFrame(frame)
ramka.columns = ['PPE ID', 'Volume Actual', 'Month', 'Year', 'Volume  Forecast' , 'Volume difference'  ]
ramka['Financial Year']=ramka.apply(f,axis=1)
pivot=pd.pivot_table(ramka,index=['Financial Year','Year','Month'], values=['Volume Actual', 'Volume Forecast' , 'Volume difference'], aggfunc=np.sum, margins=True)

Any ideas? Thank you in advance for any help.

camille
  • 16,432
  • 18
  • 38
  • 60
Pejczi
  • 13
  • 4
  • Post dataframes as text not as images. – user3483203 May 28 '18 at 15:18
  • what is the function `f` doing? Could you please provide a [Minimal, Complete, and Verifiable Example (MCVE)](https://stackoverflow.com/help/mcve)? – tobsecret May 28 '18 at 15:19
  • @tobsecret function f contains if statements basing on year and month in order to add 'financial year' column. It might be treated as regular column. Sorry , it is my first post on Stackoverflow , I didn't know how to shorten it more. – Pejczi May 28 '18 at 15:23
  • Hey, no problem! Here is my favourite answer as to how to ask a good pandas question on Stackoverflow: https://stackoverflow.com/a/32536193/7480990 The thing is as a community, it is much easier for us to help you if we can just copy and paste code from your answer and play around with it to fix it. That does not work if we don't have the actual data. You don't have to post the true data of course, just enough to reproduce your problem. The best way in my opinion to do that is to use the `.to_dict` method as explained in the link above. – tobsecret May 28 '18 at 15:31

0 Answers0