I have a wide time series dateframe with dates as headers and a column with dates. I have been
trying to add the QTYs column to the matching dates in the headers and to the 2 previous column
based on the date from the WIP column. Your feedback or advise on how to go about this will be
truly appreciated.
I have tried creating a pivot table from the QTY and WIP using the dates as columns and then
grouping by header, but I have no success.
dict = {'SKU':['a1','a2', 'a3', 'b1', 'b2', 'b3'],
'4/12/2020':[-5,-18,-25,7,20,0],
'4/19/2020':[-10,0,-20,15,10,-5],
'4/26/2020':[-15,25,-28,20,30,2],
'5/03/2020':[2,-5,-30,-5,40,5],
'WIP':['4/12/2020','4/19/2020','4/26/2020','5/03/2020','5/03/2020','4/19/2020'],
'QTY':[10,0,20,100,5,40]}
df = pd.DataFrame(dict)
df
SKU 4/12/2020 4/19/2020 4/26/2020 5/03/2020 WIP QTY
a1 -5 -10 -15 2 4/12/2020 10
a2 -18 0 25 -5 4/19/2020 0
a3 -25 -20 -28 -30 4/26/2020 20
b1 7 15 20 -5 5/03/2020 100
b2 20 10 30 40 5/03/2020 5
b3 0 -5 2 5 4/19/2020 40
SKU 4/12/2020 4/19/2020 4/26/2020 5/03/2020 WIP QTY
a1 5 -10 -15 2 4/12/2020 10
a2 -18 0 25 -5 4/19/2020 0
a3 -5 0 -8 -30 4/26/2020 20
b1 7 115 120 95 5/03/2020 100
b2 20 15 35 45 5/03/2020 5
b3 40 35 2 5 4/19/2020 40
For example,
SKU 4/12/2020 4/19/2020 4/26/2020 5/03/2020 WIP QTY
a3 -25+20 -20+20 -28+20 -30 4/26/2020 20
b1 7 15+100 20+100 -5+100 5/03/2020 100
b3 0+40 -5+40 2 5 4/19/2020 40
Try this, seems to get the correct output.
import pandas as pd
dict = {'SKU':['a1','a2', 'a3', 'b1', 'b2', 'b3'],
'4/12/2020':[-5,-18,-25,7,20,0],
'4/19/2020':[-10,0,-20,15,10,-5],
'4/26/2020':[-15,25,-28,20,30,2],
'5/03/2020':[2,-5,-30,-5,40,5],
'WIP':['4/12/2020','4/19/2020','4/26/2020','5/03/2020','5/03/2020','4/19/2020'],
'QTY':[10,0,20,100,5,40]}
df = pd.DataFrame(dict)
print(df)
for i, row in df.iterrows():
for col in df.columns[1:-2]:
if pd.Timestamp(row['WIP']) >= pd.Timestamp(col):
df.at[i, col] += row['QTY']
print(df)
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.