Here is my code:
import StringIO
from pandas import *
import numpy as np
df = read_csv(StringIO.StringIO('''Col1 Col2 A B
A D 1 6
A E 2 7
B D 3 8
B E 4 9
C D 5 19'''), delimiter='\t')
df['buc1'] = cut(df['A'], bins = [0, 2, 6, 8])
aggFunc = {'A': sum,
'B': np.average
}
After running:
df.groupby(['buc1']).agg(aggFunc)
I get:
A B
buc1
(0, 2] 3 6.5
(2, 6] 12 12.0
(6, 8] NaN NaN
My questions are:
- How do I get rid of the bottom (6, 8] bucket, given that there are no values there?
- How do I add a grand total row at the bottom, as in a pivot table in Excel?
For instance:
buc1 A B
(0, 2] 3 6.5
(2, 6] 12 12
Total 15 9.8
Note that the total row for the second column would be the average, not the sum.