This my current df. I would like to transform the dataframe in 3 steps. I need to remove duplicate time stamps but would like to keep the largest values or smallest values depending on the "Side" column. Please help :)
I have tried df= df[~df.index.duplicated(keep='first')] however this doesn't have the option to keep max or min values
The type of the index is datetime format, Price is float and Side is integer, the data frame has 8000+ rows.
Price Side
2021-12-13 00:00:03.285 51700 4
2021-12-13 00:00:03.315 51675 3
2021-12-13 00:00:03.333 50123 4
2021-12-13 00:00:03.333 50200 3
2021-12-13 00:00:03.333 50225 3
2021-12-13 00:00:03.333 50250 3
2021-12-13 00:00:03.421 50123 4
2021-12-13 00:00:03.421 50117 4
2021-12-13 00:00:03.421 50110 4
2021-12-13 00:00:03.671 50100 3
- If the time is duplicated, keep the highest value if the side is "3", if the time is duplicated and the side is "4" keep lowest value.
Desired Output:
Price Side
2021-12-13 00:00:03.285 51700 4
2021-12-13 00:00:03.315 51675 3
2021-12-13 00:00:03.333 50123 4
2021-12-13 00:00:03.333 50250 3
2021-12-13 00:00:03.421 50110 4
2021-12-13 00:00:03.671 50100 3
- Create new columns "3" and "4" with the corresponding prices
Desired Output:
Price 3 4
2021-12-13 00:00:03.285 51700 0 51700
2021-12-13 00:00:03.315 51675 51675 0
2021-12-13 00:00:03.333 50123 0 50123
2021-12-13 00:00:03.333 50250 50250 0
2021-12-13 00:00:03.421 50110 0 50110
2021-12-13 00:00:03.671 50100 50100 0
- Fill in the blanks with previous values from the same column
Desired Output:
Price 3 4
2021-12-13 00:00:03.285 51700 0 51700
2021-12-13 00:00:03.315 51675 51675 51700
2021-12-13 00:00:03.333 50123 51675 50123
2021-12-13 00:00:03.333 50250 50250 50123
2021-12-13 00:00:03.421 50110 50250 50110
2021-12-13 00:00:03.671 50100 50100 50110