Reputation: 981
I have a time-series DataFrame of sales, I need to calculate the average share of sales for each day of the week. Below is an example of what I want:
df1
: time series dataframe for sales.share_sales_week_of_day
- wanted DataFrame. For Mondays I have calculated real value. Here 28
and 42
= total week sales for every week from dataframe, 1
and 8
= sales on Mondays.Code:
df1 = pd.DataFrame(index = ['2011-01-31', '2011-02-01', '2011-02-01', '2011-02-02', '2011-02-03', '2011-02-04', '2011-02-05', '2011-02-06', '2011-02-07', '2011-02-08', '2011-02-09', '2011-02-10', '2011-02-11', '2011-02-12'],
data = [1,2,3,4,5,6,7, 8,8,8,3,4,5,6], columns = ['sales'])
share_sales_week_of_day = pd.DataFrame(data = {'d_1' : [0.5*(1.0/28 + 8.0/42)],'d_2' : [0], 'd_3' : [0], 'd_4' : [0], 'd_5' : [0], 'd_6' : [0], 'd_7' : [0]})
Can you explain how can I calculate shares for whole data....
Upvotes: 1
Views: 1068
Reputation: 323376
Let us do pd.crosstab
df.index=pd.to_datetime(df.index)
s=pd.crosstab(index=df1.index.strftime('%U'),columns=df1.index.weekday,values=df1.sales.values,aggfunc='sum',normalize='index').mean()
col_0
0 0.113095
1 0.184524
2 0.107143
3 0.136905
4 0.166667
5 0.196429
6 0.095238
dtype: float64
s.to_frame().T
col_0 0 1 2 3 4 5 6
0 0.113095 0.184524 0.107143 0.136905 0.166667 0.196429 0.095238
Upvotes: 1