0

Suppose I have a pandas table like this:

A  B
1  1
1  5
1  2
2  3
2  2
2  1

I would like to create order column B by increasing value by the group in A - but also create a third column C whereby this is the cumulative sum of B by group in A, viz:

A  B  C
1  1  1
1  2  3
1  5  8
2  1  1
2  2  3
2  3  6

I'm assuming the first part of getting the column ascending can be covered off by the code below, but unsure about the 2nd step of the cumulative sum of B in a new column C. Thanks in advance...!

df = df.sort_values(['B'],ascending=True).groupby('A')

0 Answers0