Python 根据表中的条件,将列名列表作为新列返回
我的每个客户和产品的数据如下所示:Python 根据表中的条件,将列名列表作为新列返回,python,python-3.x,pandas,Python,Python 3.x,Pandas,我的每个客户和产品的数据如下所示: Customer P1 P2 P3 P4 P5 P6 c1 10 2 43 21 11 4 c2 1 3 32 1 6 3 c3 20 4 20 72 78 80 c4 30 80 31 31 29 20 我希望输出如下: Customer P1 P2 P3 P4
Customer P1 P2 P3 P4 P5 P6
c1 10 2 43 21 11 4
c2 1 3 32 1 6 3
c3 20 4 20 72 78 80
c4 30 80 31 31 29 20
我希望输出如下:
Customer P1 P2 P3 P4 P5 P6 Top_Products (based on scores)
c1 10 2 43 21 11 4 [P3,P4,P5]
c2 1 3 32 1 6 3 [P3,P5,P2]
c3 20 4 20 72 78 80 [P6,P5,P4]
c4 30 80 31 31 29 20 [P2,P3,P4]
输出说明:我对每个客户的产品分数进行水平排序,并将前三名分数的列名(降序)作为每个客户的“顶级产品”列放入一个列表中
例如,对于第1行,p3、p4和p5的得分最高(按最佳得分排序),并作为列表放入另一列中首先使用
iloc
获取所有p
列,然后获取排序值的位置,使用索引并最后将值转换为列表:
df1 = df.iloc[:, 1:]
df['Top_Products'] = df1.columns.values[np.argsort(df1.to_numpy(), axis=1)[:, :3]].tolist()
print (df)
Customer P1 P2 P3 P4 P5 P6 Top_Products
0 c1 10 2 43 21 11 4 [P2, P6, P1]
1 c2 1 3 32 1 6 3 [P1, P4, P2]
2 c3 20 4 20 72 78 80 [P2, P1, P3]
3 c4 30 80 31 31 29 20 [P6, P5, P1]
df['Top_Products'] = df1.apply(lambda x: x.nsmallest(3).index.tolist(), axis=1)
print (df)
Customer P1 P2 P3 P4 P5 P6 Top_Products
0 c1 10 2 43 21 11 4 [P2, P6, P1]
1 c2 1 3 32 1 6 3 [P1, P4, P2]
2 c3 20 4 20 72 78 80 [P2, P1, P3]
3 c4 30 80 31 31 29 20 [P6, P5, P1]
如果性能不重要或行数较少,请与“将索引转换为列表”一起使用:
df1 = df.iloc[:, 1:]
df['Top_Products'] = df1.columns.values[np.argsort(df1.to_numpy(), axis=1)[:, :3]].tolist()
print (df)
Customer P1 P2 P3 P4 P5 P6 Top_Products
0 c1 10 2 43 21 11 4 [P2, P6, P1]
1 c2 1 3 32 1 6 3 [P1, P4, P2]
2 c3 20 4 20 72 78 80 [P2, P1, P3]
3 c4 30 80 31 31 29 20 [P6, P5, P1]
df['Top_Products'] = df1.apply(lambda x: x.nsmallest(3).index.tolist(), axis=1)
print (df)
Customer P1 P2 P3 P4 P5 P6 Top_Products
0 c1 10 2 43 21 11 4 [P2, P6, P1]
1 c2 1 3 32 1 6 3 [P1, P4, P2]
2 c3 20 4 20 72 78 80 [P2, P1, P3]
3 c4 30 80 31 31 29 20 [P6, P5, P1]
编辑:对于得分最高的前三名值,答案非常相似,只需为-df1添加-
我们可以使用:
你好@jezrael,谢谢你的超级回答。我已经编辑了这个问题,现在我想让列表按最高分数的顺序排列,它向我显示错误:“一元操作数类型错误-:list”@wighty-How-workingdf1.columns.values[np.argsort(-df1.to_numpy(),axis=1)[:,:3]
?错误消失了,但现在它给了我名单上的顾客too@Mighty-您是否使用df1=df.iloc[:,1::
删除列Customer
?