How to do a weighted sum when using groupBy in pandas

Viewed 2046

I have made up an example because the context and details of my dataset might be too much/unnecessary to explain to deliver my question. While my example might be silly, just know that the example does illustrate what I am hoping to achieve (although at a much much larger scale) and is very important to the given problem. In this example, imagine we have different users (denoted by an alphabetical letter). Each user share multiple posts, and different users often times share the same post. We then come up with a importance score (either 0 or 1 indicating whether important) and reliability score (on a scale from 1-10). Although it is completely irrelevant how these metrics are computed for the sake of this question, imagine importance perhaps analyzes content and context/current events, and reliability takes into account prior performance of this source/user. It is not clear as to whether there is a relationship between importance and reliability

User       Share                      Importance            Reliability

 A         Carrots are                     0                     3
           good for eyesight

 B         Apple Cider Vinegar             1                     4
           is good for pain

 C         Garlic is good for breadth      0                     7

 A         Garlic is good for breadth      1                     6
 B         Carrots are good for eyesight   1                     9

The numbers might not make sense -- apologies Regardless, I want to do some sort of weighted sum for each text that takes into account the reliability and importance. To do so, I want to find each unique text (indicated by the share column) and sum the product of the importance and reliability scores for all the users who have shared that text. Therefore, I get something like:

A   6
B   13
C   0

I would love sample code and suggestions as to how to solve this question! Thanks in advance.

4 Answers
Related