简体   繁体   中英

How to convert weeks into months and sum values

I've a dataset such as follows

Index Sku Week Sales

0 SKU1442 201420 1904.0

1 SKU1442 201421 692.0

2 SKU1442 201422 842.0

3 SKU1442 201423 1013.0

4 SKU1442 201424 362.0

5 SKU1442 201425 279.0

6 SKU1442 201426 430.0

7 SKU1442 201427 861.0

8 SKU1442 201428 1069.0

9 SKU1442 201429 721.0

I'm trying to sum sales across the months based on the year and week numbers. for eg 201406 ->9999, 201407 -> 100

So far I've been able to convert the week into date using function as;

dt = datetime.strptime('201420'+ '1', '%Y%W%w')

Any help is appreciated.

I think need to_datetime first and then resample and aggregate sum :

df['Week'] = pd.to_datetime(df['Week'].astype(str) + '1', format='%Y%W%w')

df = df.resample('M', on='Week').sum()
print (df)
             Sales
Week              
2014-05-31  2596.0
2014-06-30  2926.0
2014-07-31  2651.0

Alternative solution is convert to month period by to_period :

df = df.groupby(df['Week'].dt.to_period('m')).sum()
print (df)
          Sales
Week           
2014-05  2596.0
2014-06  2926.0
2014-07  2651.0

EDIT: For better sample was changed input data in ISku column:

print (df)
      ISku    Week   Sales
0  SKU1442  201420  1904.0
1  SKU1442  201421   692.0
2  SKU1442  201422   842.0
3  SKU1442  201423  1013.0
4  SKU1444  201424   362.0
5  SKU1444  201425   279.0
6  SKU1444  201426   430.0
7  SKU1444  201427   861.0
8  SKU1444  201428  1069.0
9  SKU1442  201429   721.0

df['Week'] = pd.to_datetime(df['Week'].astype(str) + '1', format='%Y%W%w')

df = df.groupby(['ISku',df['Week'].dt.to_period('m')]).sum().reset_index()
print (df)
      ISku    Week   Sales
0  SKU1442 2014-05  2596.0
1  SKU1442 2014-06  1855.0
2  SKU1442 2014-07   721.0
3  SKU1444 2014-06  1071.0
4  SKU1444 2014-07  1930.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM