我有以下代表加密交易的数据:
1599177600000,381.52,1.425,s
1599177600000,381.49,0.828,s
1599177600000,381.48,0.747,s
1599177600212,381.53,3.225,s
1599177600560,381.53,0.226,s
1599177600560,381.45,0.637,s
1599177600560,381.44,11.431,s
1599177600560,381.38,2.153,s
1599177600560,381.37,0.569,s
1599177600560,381.35,150,s
1599177600560,381.33,1.056,s
1599177600560,381.32,8.581,s
1599177600560,381.31,16.947,s
1599177600560,381.29,15.877,s
1599177600590,381.45,2.586,s
1599177600652,381.54,0.03,b
1599177600826,381.39,0.5,s
1599177601166,381.39,0.139,s
1599177601304,381.39,1.445,s
1599177601306,381.35,2.555,s
1599177601624,381.3,1.552,s
1599177601706,381.29,2,s
1599177601868,381.31,0.262,s
1599177602108,381.29,0.092,s
1599177602242,381.3,0.05,b
1599177602296,381.31,2.228,b
1599177602312,381.32,0.05,b
1599177602386,381.33,0.639,b
1599177602388,381.29,7.901,s
1599177602388,381.25,12.099,s
这些列是:unix 时间戳(毫秒)、价格、数量和代表交易是买入还是卖出事件的字母(b 或 s)。
使用 Pandas,如何将具有相同时间戳的行合并在一起,同时添加额外的列?
合并规则为:
new quantity = sum quantity for all rows
new price = sum (quantity * price) for all rows / new quantity
例外的是:
if there is a duplicate timestamp with different letters, the one with the letter 'b' has to be pushed ahead by 1ms
额外的列是:
if a row is a result of a merge, the extra columns needs to have a bool True in it
然后使用该时间戳作为索引?
我不确定这是否可以一次性完成,但我也不太熟悉 Pandas 的语法来弄清楚如何做到这一点,所以任何带有解释的答案都会很棒。
杨魅力
30秒到达战场
相关分类