使用seaborn和pandas每张发票购买的商品数量

时间:2017-12-08 15:49:40

标签: python pandas seaborn

我正在使用online retail dataset,我想绘制每个发票号码的项目数量分布。

enter image description here

InvoiceNo类型:非空对象

类型数量:非null int64

怎么做?此外,我不知道如何使用pandas操作进行过滤,以获得没有可视化的操作。 例如,如果我想知道我可以做的每张发票的数量:

retail_uk.groupby('InvoiceNo').sum().sort_values(by='Quantity', ascending=False)['Quantity']

我需要一个答案:

  • 如何用Seaborn绘制(更重要的是因为它可以让我更好地了解分布)实现这一目标需要哪些不同的数据转换?

  • 如何使用Pandas的groupby进行操作。

2 个答案:

答案 0 :(得分:1)

我认为你需要汇总sum和情节seaborn.barplot

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00352/Online%20Retail.xlsx'
retail_uk = pd.read_excel(url)

df = retail_uk.groupby('InvoiceNo', as_index=False)['Quantity'].sum()
#if want sorting
df = df.sort_values(by='Quantity', ascending=False)
print (df.head())

ax = sns.barplot(x="InvoiceNo", y="Quantity", data=df)

答案 1 :(得分:0)

如果您想知道每个唯一InvoiceNo的行数,您可以

df.groupby('InvoiceNo').size()

如果您想将所有唯一的InvoiceNo组合在一起并对每个组中的“数量”行求和,那么

df.groupby('InvoiceNo').agg(np.sum)['Quantity']