I am grouping a dataframe by 2 columns and i aggregate by the sum of the other columns. How I can have a total by the first grouped column in the same data frame?
for example my data frame is:
np.random.seed(0)
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C' : np.random.randn(8),
'D' : np.random.randn(8)})
The result of:
grouped = df.groupby(by=['A', 'B']).sum()
is:
C D
A B
bar one 0.400157 0.410599
three 2.240893 1.454274
two -0.977278 0.121675
foo one 2.714141 0.340644
three -0.151357 0.333674
two 2.846296 0.905081
I what to get:
C D
A B
bar one 0.400157 0.410599
two -0.977278 0.121675
three 2.240893 1.454274
total 1.663773 1.986547
foo one 2.714141 0.340644
two 2.846296 0.905081
three -0.151357 0.333674
total 5.409080 1.579400
how it can be done?
UPDATE: I found a similar question at Pandas groupby and sum total of group It has 2 more answer for this question.