How do I add column values as long as a value in the other column is the same?
e.g. from the following list:
Time Value
10 a
20 b
10 c
10 d
20 f
I want to obtain the following result:
Time Value
10 (a+c+d)
20 (b+f)
You can use groupby
, then select the Value
elements (so you don't sum
the Times
), then finally apply
sum
.
>>> d = {'Time': [10,20,10,10,20], 'Value': ['a', 'b', 'c', 'd', 'f']}
>>> df = pd.DataFrame(d)
>>> df
Time Value
0 10 a
1 20 b
2 10 c
3 10 d
4 20 f
>>> df.groupby(['Time'])['Value'].apply(sum).reset_index()
Time Value
0 10 acd
1 20 bf