Please help tidy up the data frame.


Original data frame:

price qty side status tradeID date 2018-09-03 17:00:00 7282.5 10.0 Buy filled 71ZNeXwSQUqkxhKR9trvrQ 2018-09-05 11:00:00 7111.0 10.0 Sell filled 71ZNeXwSQUqkxhKR9trvrQ 2018-09-08 10:00:00 6448.0 10.0 Buy filled WYgKLRv+Q9CuXic4FNEh0A 2018-09-08 18:00:00 6377.0 10.0 Sell filled WYgKLRv+Q9CuXic4FNEh0A 2018-09-09 14:00:00 6376.5 10.0 Buy filled /6WmcfJ1QcWWwPcwkXeoSw 

Requires tradeid obedience. Ie, that the lines with the same tradeID are combined by this value. An example of the desired final data frame.

  price qty side status tradeID date 71ZNeXwSQUqkxhKR9trvrQ 2018-09-03 17:00:00 7282.5 10.0 Buy filled 2018-09-05 11:00:00 7111.0 10.0 Sell filled WYgKLRv+Q9CuXic4FNEh0A 2018-09-08 10:00:00 6448.0 10.0 Buy filled 2018-09-08 18:00:00 6377.0 10.0 Sell filled /6WmcfJ1QcWWwPcwkXeoSw 2018-09-09 14:00:00 6376.5 10.0 Buy filled 

Thanks a lot in advance!

    1 answer 1

    Use the DataFrame.set_index () method:

     In [251]: df2 = df.set_index(['tradeID', df.index]) In [252]: df2 Out[252]: price qty side status tradeID date 71ZNeXwSQUqkxhKR9trvrQ 2018-09-03 17:00:00 7282.5 10.0 Buy filled 2018-09-05 11:00:00 7111.0 10.0 Sell filled WYgKLRv+Q9CuXic4FNEh0A 2018-09-08 10:00:00 6448.0 10.0 Buy filled 2018-09-08 18:00:00 6377.0 10.0 Sell filled /6WmcfJ1QcWWwPcwkXeoSw 2018-09-09 14:00:00 6376.5 10.0 Buy filled 
    • Thank you very much, I was the one who tried to do it =) - Dmitry Sharko
    • @DmitrySharko, please :) - MaxU