I have a date frame with 3 columns, including some duplicate rows:
dict1 = {'experiment': ['A', 'B', 'B', 'C', 'C', 'C', 'C'], 'run': ['A-1', 'B-1', 'B-2', 'C-1', 'C-1', 'C-2', 'C-2'], 'data': [6, 5, 5, 4, 4, 4, 4]}
df1 = pd.DataFrame(data=dict1)
print(df1)
experiment run data
0 A A-1 6
1 B B-1 5
2 B B-2 5
3 C C-1 4
4 C C-1 4
5 C C-2 4
6 C C-2 4
I am trying to create a new column that, for each row, contains the sum of column 'data' of unique runs for that experiment. The duplicate rows should stay intact. So my expected outcome is:
experiment run data exp-sum
0 A A-1 6 6
1 B B-1 5 10
2 B B-2 5 10
3 C C-1 4 8
4 C C-1 4 8
5 C C-2 4 8
6 C C-2 4 8
I have tried combining .groupby and .unique, but so far I only get the correct sums per run, which would need to be further summed up (per experiment) and then written into the original df.
print(df1.groupby('run')['data'].unique())
run
A-1 [6]
B-1 [5]
B-2 [5]
C-1 [4]
C-2 [4]
Any input very welcome!