Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中绘制连续购买之间的月数(装箱)和每个箱子中的客户数量_Python_Python 3.x_Pandas_Plotly - Fatal编程技术网

在python中绘制连续购买之间的月数(装箱)和每个箱子中的客户数量

在python中绘制连续购买之间的月数(装箱)和每个箱子中的客户数量,python,python-3.x,pandas,plotly,Python,Python 3.x,Pandas,Plotly,我的数据如下所示: Customer Product Date C1 P1 2012-01-02 C1 P1 2014-02-21 C1 P1 2016-05-22 C2 P1 2012-02-20 C2 P1 2013-08-02 C1 P1

我的数据如下所示:

Customer     Product      Date
C1           P1           2012-01-02
C1           P1           2014-02-21      
C1           P1           2016-05-22
C2           P1           2012-02-20
C2           P1           2013-08-02
C1           P1           2017-06-02
C1           P1           2018-07-11
我必须为每个客户计算两次连续购买之间的月数(箱数),并绘制箱数和每个箱中的客户数之间的图表

附上excel中图表的示例。这里x轴表示连续购买的月份数,y轴表示每个箱子中的客户数

基本上,步骤是为每个客户找到两次购买之间的平均月数,bin月数,并绘制月数bin和每个bin中的客户数之间的条形图

“月”是一个持续28到31天的可变时间量。在我们的分析中,让我们将一个月定义为相当于30天。然后,您可以使用
pd.cut
对它们进行装箱并制作直方图:

s = df.sort_values(['Customer', 'Date']) \
      .groupby('Customer').apply(lambda g: g['Date'].diff().div(pd.Timedelta(days=30)).mean())

t = pd.cut(s, [10, 15, 20, 30, np.inf], labels=['10-15M', '15-20M', '20-30M', '>30M'])
t.hist()
结果(示例中的两个客户都属于
15-20M
bin):


是客户购买产品之间的时差,还是产品内的时差?时差在产品内。我有8种不同的产品,我必须为每种产品建立一个图表