![](/img/trans.png)
[英]GroupBy one column, custom operation on another column of grouped records in pandas
[英]Applying an operation efficiently on one column that depends on another column with pandas
我有一个名为df
的 Dataframe 大约有 20m 行,看起来像
userId movieId rating
0 1 296 5.0
1 1 306 3.5
2 1 307 5.0
3 2 665 5.0
4 2 899 3.5
...
我有一个系列, user_bias
userId
1 0.280431
2 0.096580
3 0.163554
4 -0.155755
5 0.218621
...
我想根据user_bias
中的userId
列从df['rating']
中减去匹配值。 例如,第一行的评级值应替换为5.0 - 0.280431 = 4.719569
。 我尝试了两种解决方案,但它们似乎很慢。 有没有更好的方法来实现这一目标?
for i, row in df.iterrows():
df.at[i, 'rating'] -= user_bias[row.userId]
为了摆脱 for 循环,我使用了apply
方法。 不确定结果是否正确,但它再次比我预期的要慢。
df['rating'] = df.apply(lambda row: row.rating - user_bias[row.userId], axis=1)
尝试reindex
df['rating'] = df['rating'] - user_bias.reindex(df['userId']).values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.