I'm learning to use python and pandas and I'm wondering how can I combine something from different rows based on the cell values in two columns. In this case, i want to combine scores from same department and within same id.
This is the simplified version of the DF I have:
id department name scoreA
abc sales eric 2
abc sales jack 3
abc marketing sofia 6
abc marketing anna 7
zzz sales jack 1
zzz sales eric 8
zzz marketing sofia 11
zzz marketing anna 1
And this is the DF I want:
id department totalScoreA
abc sales 5
abc marketing 13
zzz sales 9
zzz marketing 12
I have also a follow up question. What if there's two columns for counts and I want an average of those counts, but before averaging those values I want to multiply the scoreB by 2. Like this:
id department name scoreA scoreB
abc sales eric 2 10
abc sales jack 3 6
abc marketing sofia 6 8
abc marketing anna 7 10
zzz sales eric 8 10
zzz sales jack 2 10
zzz marketing sofia 11 4
zzz marketing anna 1 10
And this is the DF I want:
id department totalScoreA AverageScore((A+B*2)/2)
abc sales 5 18.5
abc marketing 13 24.5
zzz sales 10 25
zzz marketing 12 20
Update:
Hey thank you so much for the answers @jezrael! The first one worked as it should!
However I might've been little bit too vague for defining the second question. What I wanted is to get an "combined" mean of all the scoreB*2's ScoreA's for every group of every department. I give an example with values to clarify this:
From this:
id department name scoreA scoreB
zzz marketing sofia 5 4
zzz marketing anna 2
To this:
The meanAB is (5+2+4*2)/3 (the number 3 comes from the count of the values). So how would i calculate that one because i wasn't able to do it, even with the help of your previous solution :/
id department meanA meanB meanAB
zzz marketing 3.5 4 5