I am new to Pandas and I am trying to do the following thing::
- I have a dataframe called comms with columns articleID and commentScore (among others)
- I have another dataframe called arts with column articleID
I need to create in arts a new column called articleScore. Each article must have the articleScore which is the sum of all commentScores related to that article (same articleID), divided by sqrt(n_comms + 1), where n_comms is the number of comments with that specific ID.
I already managed to do this but In a very inefficient way (pictured below)
for article in arts:
n, tempScore = 0
for i, value in comms.iterrows():
if value['articleID'] == article['articleID']:
tempScore + = value['commentScore']
n += 1
article['articleScore'] /= math.sqrt(n+1)
Edit: Here's an example of what I would like to happen:
comms:
__________________________
| # | artID | commScore |
| 0 | 1x5w | 2 |
| 1 | 77k3 | 1 |
| 2 | 77k3 | -1 |
| 3 | 3612 | 5 |
| 4 | 1x5w | 3 |
--------------------------
arts:
___________________________
| # | artID | artScore (?) |
| 0 | 1x5w | 2.89 |
| 1 | 77k3 | 0 |
| 2 | 3612 | 3.54 |
-------------------------
I need to (create and) fill the artScore column. Each artScore is the sum of the commentScores, but only of the comments with the same artID of the article, divided by sqrt(n+1).
Can anybody help me? Thanks a lot!
Andrea