我需要编写代码,为我提供一张表格,其中包含最受欢迎的商品和利润最高的商品。 下面的代码似乎运行良好,我只需要重复一下,以便第一个按价格计数排序,第二个按价格总和排序。
most_popular_item = Heroes_file.groupby(["Item ID", "Item Name","Price"]).agg({"Price": ['count','sum']})
most_popular_item.head()
THIS WILL PRINT:
Item ID Item Name Price
0 Splinter 1.28 4 5.12
1 Crucifer 3.26 3 9.78
2 Verdict 2.48 6 14.88
3 Phantomlight 2.49 6 14.94
4 Bloodlord's Fetish 1.70 5 8.50
我正在使用此代码,但出现错误(#检查重复项)
KeyError: 'Price count'
most_popular_item = most_popular_item.sort_values(["Price count"], ascending=False)
答案 0 :(得分:1)
.agg({"Price": ['count','sum']})
使用列创建一个多索引。要对多索引进行排序,您还需要使用类似这样的元组:
most_popular_item = most_popular_item.sort_values(by = ('Price','count'), ascending=False)
答案 1 :(得分:0)
我不知道我是否在正确地阅读您想要的内容,但是您可以使用.nlargest()获得一个具有最大计数和总和的表
target_indices = most_popular_item['count'].nlargest(1).index.values.tolist() # [2]
target_indices.extend(most_popular_item['sum'].nlargest(1).index.values.tolist()) # [2, 2]
most_popular_item[most_popular_item.index.isin(target_indices)]
输出:
Item_ID Item_Name Price count sum
2 2 Verdict 2.48 6 14.98
该项目在您的小组中最受欢迎,总金额最高。