根据列合并行,直到条件为真并应用公式?

时间:2019-05-06 12:43:16

标签: python pandas dataframe

df

    Unnamed: 0  Trade Date             Symbol Exchange Segment Trade Type  Qty     Price          Order ID  Trade ID                Time
0          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell  140  29593.00  1400000000087742  50013671 2019-04-24 09:15:16
1          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29592.95  1400000000087742  50013672 2019-04-24 09:15:16
2          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29592.90  1400000000087742  50013673 2019-04-24 09:15:16
3          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29588.40  1400000000087742  50013674 2019-04-24 09:15:16
4          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29576.45  1400000000224462  52308594 2019-04-24 13:55:12
5          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy  100  29576.45  1400000000224462  52308595 2019-04-24 13:55:12
6          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29576.80  1400000000224462  52308596 2019-04-24 13:55:12
7          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   40  29576.80  1400000000224462  52308597 2019-04-24 13:55:12
8          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29577.00  1400000000224462  52308598 2019-04-24 13:55:12
9          NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29685.00  1400000033885922  52664935 2019-04-24 14:03:38
10         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29685.00  1400000033885922  52664936 2019-04-24 14:03:38
11         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29685.00  1400000033885922  52664937 2019-04-24 14:03:38
12         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29685.00  1400000033885922  52664938 2019-04-24 14:03:38
13         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy   20  29686.45  1400000033885922  52664939 2019-04-24 14:03:38
14         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO        buy  100  29686.55  1400000033885922  52664940 2019-04-24 14:03:38
15         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29915.65  1400000048617426  54163123 2019-04-24 15:16:48
16         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29915.60  1400000048617426  54163124 2019-04-24 15:16:48
17         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   40  29914.55  1400000048617426  54163125 2019-04-24 15:16:48
18         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   40  29914.45  1400000048617426  54163126 2019-04-24 15:16:48
19         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29914.00  1400000048617426  54163127 2019-04-24 15:16:48
20         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29914.00  1400000048617426  54163128 2019-04-24 15:16:48
21         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29913.95  1400000048617426  54163129 2019-04-24 15:16:48
22         NaN  2019-04-24  BANKNIFTY19APRFUT      NSE      FO       sell   20  29913.55  1400000048617426  54163130 2019-04-24 15:16:48

我要检查第0行中的Trade Type,如果下一行(1)也是相同的Trade Type,则将数量Qty添加到第一行,并使{ {1}} =基于Price的两个价格的平均值。

接下来,直到我们遇到另一个Qty

然后做同样的事情,直到不同的Trade Type的数量等于第0行为止。如果还有更多数量,我们将创建一个带有剩余数量的新行。

预期输出:

Trade Type

Unnamed: 0 Trade Date Symbol Exchange Segment Trade Type Qty Price Order ID Trade ID Time 0 NaN 2019-04-24 BANKNIFTY19APRFUT NSE FO sell 200 29592.525 1400000000087742 50013671 2019-04-24 09:15:16 1 NaN 2019-04-24 BANKNIFTY19APRFUT NSE FO buy 200 29576.61 1400000000224462 52308594 2019-04-24 13:55:12 2 NaN 2019-04-24 BANKNIFTY19APRFUT NSE FO buy 200 29685.92 1400000000224462 52308595 2019-04-24 13:55:12 3 NaN 2019-04-24 BANKNIFTY19APRFUT NSE FO sell 200 29914.475 Trade ID Order ID并不是很重要。

我可以使用for循环和列表来执行此操作,但我认为这不是pythonic / pandas的方法。

TIA

0 个答案:

没有答案