简体   繁体   中英

Next “n” Days Sales for every product

I have the following dataframe:

print(dd)
dt_op      quantity   product_code
20/01/18      1            613
21/01/18      8            611
21/01/18      1            613 
...

I am trying to get the sales in the dataframe of the next "n" days , but the following code does not compute them by product_code as well:

dd["Final_Quantity"] = [dd.loc[dd['dt_op'].between(d, d + pd.Timedelta(days = 7)), 'quantity'].sum() \
                        for d in dd['dt_op']]

I would like to define dd["Final_Quantity"] as sum of df["quantity"] sold in the next "n" days , for every different product in stock;

Ultimately, for i in dt_op and product_code .

print(final_dd)
n = 7

dt_op      quantity   product_code     Final_Quantity
20/01/18      1            613               2
21/01/18      8            611               8
25/01/18      1            613               1
...

Regardless how you wanted to present the output, you can try the following codes to get total of sales for every product for every n days. Let say for every 7 days:

dd.groupby([pd.Grouper(key='dt_op', freq='7D'), 'product_code']).sum()['quantity']

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