I have a dataframe:
df =
col1 | Num |
---|---|
1 | 4 |
1 | 4 |
2 | 5 |
2 | 1 |
2 | 1 |
3 | 2 |
I want to add all the numbers and show the total.
So I will get:
col1 | Sum |
---|---|
1 | 8 |
2 | 7 |
3 | 2 |
CodePudding user response:
Try this:
df.groupby('col1').sum()
If you wanted the new column to have the name 'sum' as in your example you could do the following:
df1 = df.groupby('col1').sum()
df1.columns = ['Sum']