0

I'm trying to group data of a column A, B, C, and D on Report column.

report  A  B  C  D
1       1  0  0  0
1       0  1  0  0
2       0  0  0  1
2       0  0  1  0
3       1  0  0  0
3       0  1  0  0     
4       0  0  1  0      
4       1  0  0  0

Here is what I'm trying to achieve

report  A  B  C  D
1       1  1  0  0
2       0  0  1  1
3       1  1  0  0 
4       1  0  1  0

Is there any straight forward way to achieve the result? Thank you very much!! I appreciate any support!

Chandan
  • 571
  • 4
  • 21
Matthias Gallagher
  • 475
  • 1
  • 7
  • 20

1 Answers1

1

I believe you're looking for sum in the pandas library. The script below outputs the expected results.

import pandas as pd

report = [1, 1, 2, 2, 3, 3, 4, 4]
A = [1, 0, 0, 0, 1, 0, 0, 1]
B = [0, 1, 0, 0, 0, 1, 0, 0]
C = [0, 0, 0, 1, 0, 0, 1, 0]
D = [0, 0, 1, 0, 0, 0, 0, 0]

df = pd.DataFrame(list(zip(report, A, B, C, D)), columns = ['report', 'A', 'B', 'C', 'D'])

print(df.groupby(['report']).sum())
tarheel
  • 4,727
  • 9
  • 39
  • 52