3 回答

TA贡献1860条经验 获得超9个赞
利用:
df_new['Ratings_per_user']=df_new.groupby('userID')['userID'].transform('count')
userID ISBN rating Ratings_per_user
0 23413 1232 2.5 3
1 12321 2311 3.2 1
2 23413 2532 1.7 3
3 23413 7853 3.8 3

TA贡献1942条经验 获得超3个赞
将结果转换value_counts为dict,然后用于replace创建具有用户评分的新列
x = df['userID'].value_counts().to_dict()
df['rating_per_user'] = df['userID'].replace(x)
print(df)
输出:
userID ISBN rating rating_per_user
0 23413 1232 2.5 3
1 12321 2311 3.2 1
2 23413 2532 1.7 3
3 23413 7853 3.8 3

TA贡献1993条经验 获得超6个赞
你可以使用map:
df['Rating per user'] = df['userID'].map(df.groupby('userID')['Rating'].count())
print(df)
userID ISBN Rating Rating per user
0 23413 1232 2.5 3
1 12321 2311 3.2 1
2 23413 2532 1.7 3
3 23413 7853 3.8 3
添加回答
举报