数据框:
我想要的:
最佳答案
您也可以get_dummies
对于列 movieCater
并乘以评分:
out = df.join(df['movieCater'].str.get_dummies(",").mul(df['rating'],axis=0))
print(out)
MovieID movieCater rating Action Adventure Comedy Crime
0 1 Action,Comedy,Adventure 4 4 4 4 0
1 2 Action,Crime 3 3 0 0 3
2 3 Crime 2 0 0 0 2
为了匹配预期的输出,在加入之前删除评级列:
out = (df.drop("rating",1).join(
df['movieCater'].str.get_dummies(",").mul(df['rating'],axis=0))
如果系列非常大,您还可以考虑使用 sep=","拆分字符串,然后使用此解决方案:https://stackoverflow.com/a/51420716/9840637得到假人。最后乘以评级列。
https://stackoverflow.com/questions/67645905/