I have a DataFrame that looks like this:
data = pd.DataFrame({'id' : ['a1', 'a1', 'a1', 'a2','a2','a3','a3','a3'],
'label' : ['0','0','1','0','1','0','1','1']})
My goal is to group by id and to do some arithmetics: to calculate the number of occurrence of '0' and '1' in each group, add 1000 to each number and, finally, to divide these numbers by each other. For example, for group 'a1': '0' occurs twice, '1' occurs once, then, 2 * 1000 / 1 * 1000 = 2.
The desired DataFrame should look like this:
id number
a1 2
a2 1
a3 0,5
These SO questions helped me a lot:
Group by two columns and count the occurrences of each combination in pandas
pandas groupby count string occurrence over column
I've tried it out in different variations but still didn't reach the desired output. Any help would be very approciated.