gpt4 book ai didi

python - 带有 groupby 和条件的 Pandas 滚动总和

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:33 24 4
gpt4 key购买 nike

我有一个数据框,其中包含具有客户分析的不同商品的销售时间序列。对于每个项目和给定的一天,我要计算:

  • 我最好的客户在过去 2 天的总销售额中的份额
  • 我的主要客户(来自列表)在过去 2 天总销售额中的份额

我已经尝试过这里提供的解决方案:

示例数据框可以通过以下方式生成:

import pandas as pd
from datetime import timedelta

df_1 = pd.DataFrame()
df_2 = pd.DataFrame()
df_3 = pd.DataFrame()

# Create datetimes and data
df_1['item'] = [1, 1, 1, 2, 2, 2, 2]
df_1['date'] = pd.date_range('1/1/2018', periods=7, freq='D')
df_1['customer'] = ['a', 'b', 'c', 'a', 'b', 'b', 'c']
df_1['sales'] = [2, 4, 1, 5, 7, 2, 3]

df_2['item'] = [1, 1, 1, 2, 2, 2, 2]
df_2['date'] = pd.date_range('1/1/2018', periods=7, freq='D')
df_2['customer'] = ['b', 'b', 'c', 'a', 'a', 'c', 'a']
df_2['sales'] = [2, 3, 4, 2, 3, 5, 6]

df_3['item'] = [1, 1, 1, 2, 2, 2, 2]
df_3['date'] = pd.date_range('1/1/2018', periods=7, freq='D')
df_3['customer'] = ['b', 'c', 'a', 'c', 'b', 'a', 'b']
df_3['sales'] = [6, 5, 2, 3, 4, 5, 6]

df = pd.concat([df_1, df_2, df_3])
df = df.sort_values(['item', 'date'])
df.reset_index(drop=True)

看起来像这样:

<表类="s-表"><头>项目<日>日期 客户销售额<正文>12018-01-01一个212018-01-01b212018-01-01b612018-01-02b412018-01-02b312018-01-02c512018-01-03c112018-01-03c412018-01-03一个222018-01-04一个522018-01-04一个222018-01-04c322018-01-05b722018-01-05一个322018-01-05b422018-01-06b222018-01-06c522018-01-06一个522018-01-07c322018-01-07一个622018-01-07b6

我希望得到以下结果:

<表类="s-表"><头>项目<日>日期 sales_at_daysales_last_2_daysa_股热门分享<正文>12018-01-0110NaNNaNNaN12018-01-0212100.200.2012018-01-037220.090.0922018-01-0410NaNNaNNaN22018-01-0514100.701.0022018-01-0612240.290.4222018-01-0715260.310.50

在哪里,

a_share是过去 2 天(不包括当天)客户“a”的销售额占总销售额的份额 top_share是客户销售额的份额

top_cust = ['a', 'c'] 

列出最近 2 天(不包括当天)的总销售额

有什么想法吗?非常感谢:)

安迪

最佳答案

使用:

#custom rolling with shift first day
f = lambda x: x.rolling(2, min_periods=1).sum().shift()

#aggregate sum
df1 = df.groupby(['item','date'], as_index=False)['sales'].sum()
#apply custom rolling per groups
df1['sales_last_2_days'] = df1.groupby('item')['sales'].apply(f).reset_index(drop=True, level=0)

#filter customer a and aggregate sum
a = df[df['customer'].eq('a')].groupby(['item','date'])['sales'].sum().rename('a_share')
#add new column to original
df1 = df1.join(a, on=['item','date'])
#applt custom rolling per groups and divide
df1['a_share'] = df1.groupby('item')['a_share'].apply(f).reset_index(drop=True, level=0) / df1['sales_last_2_days']

#verys similar like before, only test membership by isin
top_cust = ['a', 'c']
a = df[df['customer'].isin(top_cust)].groupby(['item','date'])['sales'].sum().rename('top_share')
df1 = df1.join(a, on=['item','date'])
df1['top_share'] = df1.groupby('item')['top_share'].apply(f).reset_index(drop=True, level=0) / df1['sales_last_2_days']
print (df1)
item date sales sales_last_2_days a_share top_share
0 1 2018-01-01 10 NaN NaN NaN
1 1 2018-01-02 12 10.0 0.200000 0.200000
2 1 2018-01-03 7 22.0 0.090909 0.318182
3 2 2018-01-04 10 NaN NaN NaN
4 2 2018-01-05 14 10.0 0.700000 1.000000
5 2 2018-01-06 12 24.0 0.416667 0.541667
6 2 2018-01-07 15 26.0 0.307692 0.500000

如果想对天使用rolling,那就更复杂了:

df1 = df.groupby(['item','date'], as_index=False)['sales'].sum()
sales1 = (df1.set_index('date')
.groupby('item')['sales']
.rolling('2D', min_periods=1)
.sum()
.groupby('item')
.shift()
.rename('sales_last_2_days')
)
df1 = df1.join(sales1, on=['item','date'])

df2 = df[df['customer'].eq('a')].groupby(['item','date'], as_index=False)['sales'].sum()
a = (df2.set_index('date')
.groupby('item')[['sales']]
.apply(lambda x: x.asfreq('D'))
.reset_index(level=0)
.groupby('item')['sales']
.rolling('2D', min_periods=1)
.sum()
.groupby('item')
.shift()
.rename('a_share')
)
print (a)
df1 = df1.join(a, on=['item','date'])
df1['a_share'] /= df1['sales_last_2_days']

top_cust = ['a', 'c'] 

df3 = df[df['customer'].isin(top_cust)].groupby(['item','date'], as_index=False)['sales'].sum()
b = (df3.set_index('date')
.groupby('item')[['sales']]
.apply(lambda x: x.asfreq('D'))
.reset_index(level=0)
.groupby('item')['sales']
.rolling('2D', min_periods=1)
.sum()
.groupby('item')
.shift()
.rename('top_share')
)
df1 = df1.join(b, on=['item','date'])
df1['top_share'] /= df1['sales_last_2_days']

print (df1)
item date sales sales_last_2_days a_share top_share
0 1 2018-01-01 10 NaN NaN NaN
1 1 2018-01-02 12 10.0 0.200000 0.200000
2 1 2018-01-03 7 22.0 0.090909 0.318182
3 2 2018-01-04 10 NaN NaN NaN
4 2 2018-01-05 14 10.0 0.700000 1.000000
5 2 2018-01-06 12 24.0 0.416667 0.541667
6 2 2018-01-07 15 26.0 0.307692 0.500000

关于python - 带有 groupby 和条件的 Pandas 滚动总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58577070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com