Pandas How to add values ββin multiple rows based on a value in one column
I'm just wondering if I can do a sum over multiple rows if they have the same value for the same column. For example, let's say I have a data schema A:
A:
col1, col2, col3, col4
A 0.1 0.2 0.3
B 0.4 0.5 0.6
A 0.7 0.8 0.9
C 1.0 1.1 1.2
The end result should be:
col1, col2, col3, col4
A 0.8 1.0 1.2
B 0.4 0.5 0.6
C 1.0 1.1 1.2
This is because the first and third rows of data have the same value (A) for col1 ... How should I implement this?
+3
source to share
2 answers
Use groupby
with aggregation sum
:
df1 = df.groupby('col1', as_index=False).sum()
print (df1)
col1 col2 col3 col4
0 A 0.8 1.0 1.2
1 B 0.4 0.5 0.6
2 C 1.0 1.1 1.2
df1 = df.groupby('col1').sum().reset_index()
print (df1)
col1 col2 col3 col4
0 A 0.8 1.0 1.2
1 B 0.4 0.5 0.6
2 C 1.0 1.1 1.2
+2
source to share