鉴于下表是数据集,有 4 列和数据集的前 14 行,但有超过 10,000 行。在一个订单中,有多个产品被出售给一个客户。
我想知道,在所有订单中,哪对产品类别出现最多?示例(Cat1 和 Cat2)
使用任何 python 库,如 numpy、pandas 等。仅使用 python 解决
注意 - 只有 3 个类别,产品 ID 是唯一列,订单 ID 和客户 ID 不重复。
OrderID ProdID Prodcategory Client ID
4997 1 Cat 1 21
4997 2 Cat 1 22
4997 3 Cat 2 23
4997 4 Cat 3 24
2001 5 Cat 1 25
2001 6 Cat 2 26
2001 7 Cat 2 27
2001 8 Cat 2 28
2001 9 Cat 3 29
2376 10 Cat 3 30
2376 11 Cat 1 31
2376 12 Cat 2 32
2376 13 Cat 3 33
2376 14 Cat 1 34
我想要在所有订单中出现最多的对。例如订单 4997 (Cat 1, Cat 2) (Cat 2, Cat 3) 和 (Cat 1, Cat 3 ) 都出现一次。在 2001 年的订单中, (Cat 1, Cat 2) (Cat1, Cat 3) (Cat 2, Cat 3) 总共出现一次
我的方法
# Import required libraries
import pandas as pd
import matplotlib.pyplot as plt
# Read the data into the dataframe
#df = pd.read_clipboard()
df
df.columns
df["Product category "].value_counts()
mylabels = ['Cat 2', 'Cat 1', 'Cat 3']
plt.pie(df["Product category "].value_counts(), labels = mylabels)
plt.show()
但这种方法只显示,整体价值计数,而不是重复对