#python #pandas #dataframe #multi_index
Пожалуйста помогите привести в порядок датафрейм.
Исходный датафрейм:
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
Требуется объедение по tradeID. Тоесть что бы строки с одинаковым tradeID были объедененны
по этому значения.
Пример нужного итогового датафрейма.
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
Заранее большое спасибо!
Ответы
Ответ 1
Воспользуйтесь методом DataFrame.set_index(): 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
Комментариев нет:
Отправить комментарий