subtotal

Can you subtotal rows and/or columns in a pivot table?

泄露秘密 提交于 2019-11-29 04:34:25
I have a set of queries that outputs a pivot table. Is it possible to obtain row and/or column subtotals for a pivot table ? My table I am selecting from looks like this Site FormID Present Site 1 Form A Yes Site 1 Form B Yes Site 1 Form D Yes etc... My pivot table query is this SELECT * FROM (SELECT Site, COUNT(FormID) AS NumberOfForms,FormID FROM @CRFCount WHERE Present='Yes' GROUP BY Site, FormID) d PIVOT (SUM(NumberOfForms) FOR [Site] IN ([Site 1], [Site 2], [Site 3]) ) AS p; But I really want it to result in this (which of course it does not total for me) FormID Site 1 Site 2 Site 3 Total

dplyr summarize with subtotals

最后都变了- 提交于 2019-11-28 10:45:22
One of the great things about pivot tables in excel is that they provide subtotals automatically. First, I would like to know if there is anything already created within dplyr that can accomplish this. If not, what is the easiest way to achieve it? In the example below, I show the mean displacement by number of cylinders and carburetors. For each group of cylinders (4,6,8), I'd like to see the mean displacement for the group (or total displacement, or any other summary statistic). library(dplyr) mtcars %>% group_by(cyl,carb) %>% summarize(mean(disp)) cyl carb mean(disp) 1 4 1 91.38 2 4 2 116

Pivot table subtotals in Pandas

↘锁芯ラ 提交于 2019-11-28 10:21:39
I have the following data: Employee Account Currency Amount Location Test 2 Basic USD 3000 Airport Test 2 Net USD 2000 Airport Test 1 Basic USD 4000 Town Test 1 Net USD 3000 Town Test 3 Basic GBP 5000 Town Test 3 Net GBP 4000 Town I can manage to pivot by doing the following: import pandas as pd table = pd.pivot_table(df, values=['Amount'], index=['Location', 'Employee'], columns=['Account', 'Currency'], fill_value=0, aggfunc=np.sum, dropna=True) Output: Amount Account Basic Net Currency GBP USD GBP USD Location Employee Airport Test 2 0 3000 0 2000 Town Test 1 0 4000 0 3000 Test 3 5000 0 4000

Python (Pandas) Add subtotal on each lvl of multiindex dataframe

两盒软妹~` 提交于 2019-11-28 06:07:18
Assuming I have the following dataframe: a b c Sce1 Sce2 Sce3 Sce4 Sce5 Sc6 Animal Ground Dog 0.0 0.9 0.5 0.0 0.3 0.4 Animal Ground Cat 0.6 0.5 0.3 0.5 1.0 0.2 Animal Air Eagle 1.0 0.1 0.1 0.6 0.9 0.1 Animal Air Owl 0.3 0.1 0.5 0.3 0.5 0.9 Object Metal Car 0.3 0.3 0.8 0.6 0.5 0.6 Object Metal Bike 0.5 0.1 0.4 0.7 0.4 0.2 Object Wood Chair 0.9 0.6 0.1 0.9 0.2 0.8 Object Wood Table 0.9 0.6 0.6 0.1 0.9 0.7 I want to create a MultiIndex, which will contain the sum of each lvl. The output will look like this: a b c Sce1 Sce2 Sce3 Sce4 Sce5 Sce6 Animal 1.9 1.6 1.4 1.3 2.7 1.6 Ground 0.6 1.4 0.8 0.5

Can you subtotal rows and/or columns in a pivot table?

限于喜欢 提交于 2019-11-27 18:35:09
问题 I have a set of queries that outputs a pivot table. Is it possible to obtain row and/or column subtotals for a pivot table ? My table I am selecting from looks like this Site FormID Present Site 1 Form A Yes Site 1 Form B Yes Site 1 Form D Yes etc... My pivot table query is this SELECT * FROM (SELECT Site, COUNT(FormID) AS NumberOfForms,FormID FROM @CRFCount WHERE Present='Yes' GROUP BY Site, FormID) d PIVOT (SUM(NumberOfForms) FOR [Site] IN ([Site 1], [Site 2], [Site 3]) ) AS p; But I really

Pivot table subtotals in Pandas

冷暖自知 提交于 2019-11-27 03:33:58
问题 I have the following data: Employee Account Currency Amount Location Test 2 Basic USD 3000 Airport Test 2 Net USD 2000 Airport Test 1 Basic USD 4000 Town Test 1 Net USD 3000 Town Test 3 Basic GBP 5000 Town Test 3 Net GBP 4000 Town I can manage to pivot by doing the following: import pandas as pd table = pd.pivot_table(df, values=['Amount'], index=['Location', 'Employee'], columns=['Account', 'Currency'], fill_value=0, aggfunc=np.sum, dropna=True) Output: Amount Account Basic Net Currency GBP