下面是我的数据框的小片段。
UserID Recommendations
0 A001 [(B000OR5928, 5.671419620513916), (B000A1HU1G, 5.435572624206543), (B0039HBNMA, 5.4260640144348145), (B000EEGAJW, 5.502416133880615), (B001L8KE06, 5.508320331573486), (B0002ZO60I, 5.640686511993408), (B0002D0096, 5.543562412261963), (B0013PU75Y, 5.452023506164551), (B005M0TKL8, 5.481754302978516), (B001PGXHYO, 5.5017194747924805)]
1 A002 [(B000EEGAJW, 4.382242679595947), (B004ZKIHVU, 4.182255268096924), (B000CBE3GE, 4.242227077484131), (B000CCJP4I, 4.354374408721924), (B000VBC5CY, 4.342846393585205), (B0002KZHQA, 4.127199649810791), (B0026RB0G8, 4.246310234069824), (B0002D0CQC, 4.275753021240234), (B0002M6CVC, 4.679849624633789), (B0002D0KOG, 4.138158321380615)]
数据框包含两列UserID和Recommendations.recommment列包含向该用户推荐的产品的productID以及以列表形式的评级。
我想做的是,如果我单击用户A001,则应该显示一个图形。图形的y轴将显示推荐给A001的产品ID,X轴将显示该产品的等级。每个用户ID的情况
我知道如何使用matplotlib绘制具有单个值的图形,但是这里有一个值列表。我该怎么做。
答案 0 :(得分:1)
您可以尝试以下代码来解决您的问题:
import matplotlib.pyplot as plt
import numpy as np
for i in df.UserID:
ratings = []
productsIDs = []
for points in df.Recommendations[np.where(df.UserID==i)[0]]:
for point in points:
ratings.append(point[1])
productsIDs.append(point[0])
plt.plot(ratings, productsIDs)
plt.show()