返回

python-提取行并将其附加到其他行的末尾

发布时间:2022-06-19 17:44:15 257
# node.js

我有一个包含事务列表的数据框。我想做的是为每笔交易创建一个损益表,并使其适合进入另一个软件。

基本上,我想获得“买入”交易并找到下一行“卖出”交易。然后将该“卖出”交易附加到“买入”行的末尾。或者,它可以是短期操作:在这种情况下,我将进行“卖出”交易,搜索下一个(最接近的时间)“买入”交易,并将其添加到“买入”行的末尾。

输入数据框:

|       |Side|Qty|Price  |Time                     |Commission| ID       |  5min_time                |
|-------|----|---|-------|------------------------------------|--------------------------------------|
|228    |Buy |1  |4087.00|2022-06-10 00:16:00+08:00|0.4       | 727127819|  2022-06-10 00:15:00+08:00|
|229    |Buy |1  |4087.00|2022-06-10 00:16:00+08:00|0.4       | 727127819|  2022-06-10 00:15:00+08:00|
|230    |Buy |1  |4098.75|2022-06-10 00:57:00+08:00|0.4       | 727141021|  2022-06-10 00:55:00+08:00|
|231    |Sell|1  |4094.25|2022-06-10 00:56:00+08:00|0.4       | 727140488|  2022-06-10 00:55:00+08:00|
|232    |Sell|1  |4094.25|2022-06-10 00:56:00+08:00|0.4       | 727140488|  2022-06-10 00:55:00+08:00|
|233    |Buy |1  |4104.25|2022-06-10 00:59:00+08:00|0.4       | 727141284|  2022-06-10 00:55:00+08:00|
|234    |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|
|235    |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|
|236    |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|
|237    |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|
|238    |Buy |1  |4059.75|2022-06-10 03:04:00+08:00|0.4       | 727156591|  2022-06-10 03:00:00+08:00|
|239    |Buy |1  |4059.75|2022-06-10 03:04:00+08:00|0.4       | 727156591|  2022-06-10 03:00:00+08:00|

预期结果:

|       |Side|Qty|Price  |Time                     |Commission| ID       |  5min_time                |
|-------|----|---|-------|------------------------------------|--------------------------------------|
|228    |Buy |1  |4087.00|2022-06-10 00:16:00+08:00|0.4       | 727127819|  2022-06-10 00:15:00+08:00|231   |Sell|1  |4094.25|2022-06-10 00:56:00+08:00|0.4       | 727140488|  2022-06-10 00:55:00+08:00|
|229    |Buy |1  |4087.00|2022-06-10 00:16:00+08:00|0.4       | 727127819|  2022-06-10 00:15:00+08:00|232   |Sell|1  |4094.25|2022-06-10 00:56:00+08:00|0.4       | 727140488|  2022-06-10 00:55:00+08:00|
|230    |Buy |1  |4098.75|2022-06-10 00:57:00+08:00|0.4       | 727141021|  2022-06-10 00:55:00+08:00|234   |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|
|233    |Buy |1  |4104.25|2022-06-10 00:59:00+08:00|0.4       | 727141284|  2022-06-10 00:55:00+08:00|235   |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|
|236    |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|238   |Buy |1  |4059.75|2022-06-10 03:04:00+08:00|0.4       | 727156591|  2022-06-10 03:00:00+08:00|
|237    |Sell|1  |4085.75|2022-06-10 01:43:00+08:00|0.4       | 727156519|  2022-06-10 01:40:00+08:00|239   |Buy |1  |4059.75|2022-06-10 03:04:00+08:00|0.4       | 727156591|  2022-06-10 03:00:00+08:00|

想到的唯一想法是遍历所有行并将它们单独附加到其他行的末尾,但这几乎不是一种有效的方法。有人可以向我推荐一种实现预期结果的 Pythonic 方式吗?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像