My dataframe looks like this:
ID Time Quantity
1 2020-01-01 00:00:01 0
1 2020-01-01 00:00:02 500
2 2020-01-01 00:00:03 0
1 2020-01-01 00:00:04 300
1 2020-01-01 00:00:05 0
2 2020-01-01 00:00:06 200
2 2020-01-01 00:00:07 100
I would like to replace the quantity of the last row (timewise) per ID with the value of the row before - if the quantity value of the last row is 0.
so result should look like this:
ID Time Quantity
1 2020-01-01 00:00:01 0
1 2020-01-01 00:00:02 500
2 2020-01-01 00:00:03 0
1 2020-01-01 00:00:04 300
1 2020-01-01 00:00:05 300 # <--- quantity last row replaced
2 2020-01-01 00:00:06 200
2 2020-01-01 00:00:07 100 # <--- quantity last row not replaced
I kind of thought about using this code:
last=df.sort_values(by='Time').groupby('ID')['Quantity'].transform('last')
but I don't know how to combine this with replace()