我是 Pandas 的新手,我正在尝试做以下事情::
- 我有一个名为 comms 的数据框,其中包含 articleID 和 commentScore(以及其他列)
- 我有另一个名为 arts 的数据框,其中包含 articleID 列
我需要在 arts 中创建一个名为 articleScore 的新专栏。每篇文章都必须有 articleScore,它是与该文章(相同文章 ID)相关的所有 commentScores 的总和除以 sqrt(n_comms + 1),其中 n_comms 是具有该特定 ID 的评论数。
我已经做到了,但是效率很低(如下图)
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)
编辑:这是我希望发生的事情的示例:
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 |
-------------------------
我需要(创建和)填充 artScore 列。每个 artScore 是 commentScores 的总和,但只是与文章具有相同 artID 的评论除以 sqrt(n+1)。
有人能帮帮我吗? 非常感谢!
安德里亚
最佳答案
我认为您可以使用 groupby
然后在 'artID'
上进行合并:
grpd = comms.groupby('artID')
to_merge = grpd.sum().divide(np.sqrt(grpd.count()+1)).reset_index().rename(columns={'commScore': 'artScore'})[['artID', 'artScore']]
arts.merge(to_merge, on='artID')
关于python-3.x - Pandas :如何根据另一个数据框的值对数据框上的列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67139587/