熊猫群居与交错

时间:2018-07-23 23:38:17

标签: python pandas

我需要这样做:

74    buy  7395.41  0.000022  19.07.2018 16:00
75   sell  7380.11  0.011296  20.07.2018 06:45
76    buy  7503.56  0.011090  20.07.2018 14:30
77   sell  7272.64  0.011091  21.07.2018 02:30
78    buy  7363.81  0.010974  21.07.2018 14:00
79   sell  7450.58  0.010974  22.07.2018 13:45
80    buy  7497.86  0.010892  22.07.2018 17:30
81   sell  7405.44  0.010892  22.07.2018 23:45
82    buy  7605.28  0.010603  23.07.2018 02:30
83   sell  7742.51  0.001060  23.07.2018 18:23
84   sell  7743.68  0.001060  23.07.2018 18:24
85   sell  7748.71  0.001060  23.07.2018 18:24
86   sell  7751.81  0.001060  23.07.2018 18:25
87   sell  7755.07  0.001060  23.07.2018 18:25
88   sell  7758.39  0.001060  23.07.2018 18:26
89   sell  7745.54  0.001060  23.07.2018 18:26
90   sell  7748.11  0.001060  23.07.2018 18:27
91   sell  7751.05  0.001060  23.07.2018 18:27
92   sell  7748.11  0.001060  23.07.2018 18:28
93    buy  7774.71  0.001058  23.07.2018 18:32
94    buy  7779.51  0.001058  23.07.2018 18:33
95    buy  7769.85  0.001058  23.07.2018 18:33
96    buy  7748.35  0.001058  23.07.2018 18:34
97    buy  7730.00  0.001058  23.07.2018 18:34
98    buy  7649.00  0.001058  23.07.2018 18:35
99    buy  7644.99  0.001058  23.07.2018 18:35
100   buy  7696.30  0.001058  23.07.2018 18:36
101   buy  7695.00  0.001058  23.07.2018 18:36
102   buy  7702.84  0.001058  23.07.2018 18:37

此:

74    buy  7395.41  0.000022  19.07.2018 16:00
75   sell  7380.11  0.011296  20.07.2018 06:45
76    buy  7503.56  0.011090  20.07.2018 14:30
77   sell  7272.64  0.011091  21.07.2018 02:30
78    buy  7363.81  0.010974  21.07.2018 14:00
79   sell  7450.58  0.010974  22.07.2018 13:45
80    buy  7497.86  0.010892  22.07.2018 17:30
81   sell  7405.44  0.010892  22.07.2018 23:45
82    buy  7605.28  0.010603  23.07.2018 02:30
83   sell  7749.29  0.010600  23.07.2018 18:23
93    buy  7719.05  0.010580  23.07.2018 18:32

我需要通过按数量加权平均价格(列)和按数量总和的边栏(买\卖)交织来分组。 A可以通过并列显示全部内容。

1 个答案:

答案 0 :(得分:1)

>>> df
    col_1 col_2    col_3     col_4       col_5  col_6
0      74   buy  7395.41  0.000022  19.07.2018  16:00
1      75  sell  7380.11  0.011296  20.07.2018   6:45
2      76   buy  7503.56  0.011090  20.07.2018  14:30
3      77  sell  7272.64  0.011091  21.07.2018   2:30
4      78   buy  7363.81  0.010974  21.07.2018  14:00
5      79  sell  7450.58  0.010974  22.07.2018  13:45
6      80   buy  7497.86  0.010892  22.07.2018  17:30
7      81  sell  7405.44  0.010892  22.07.2018  23:45
8      82   buy  7605.28  0.010603  23.07.2018   2:30
9      83  sell  7742.51  0.001060  23.07.2018  18:23
10     84  sell  7743.68  0.001060  23.07.2018  18:24
11     85  sell  7748.71  0.001060  23.07.2018  18:24
12     86  sell  7751.81  0.001060  23.07.2018  18:25
13     87  sell  7755.07  0.001060  23.07.2018  18:25
14     88  sell  7758.39  0.001060  23.07.2018  18:26
15     89  sell  7745.54  0.001060  23.07.2018  18:26
16     90  sell  7748.11  0.001060  23.07.2018  18:27
17     91  sell  7751.05  0.001060  23.07.2018  18:27
18     92  sell  7748.11  0.001060  23.07.2018  18:28
19     93   buy  7774.71  0.001058  23.07.2018  18:32
20     94   buy  7779.51  0.001058  23.07.2018  18:33
21     95   buy  7769.85  0.001058  23.07.2018  18:33
22     96   buy  7748.35  0.001058  23.07.2018  18:34
23     97   buy  7730.00  0.001058  23.07.2018  18:34
24     98   buy  7649.00  0.001058  23.07.2018  18:35
25     99   buy  7644.99  0.001058  23.07.2018  18:35
26    100   buy  7696.30  0.001058  23.07.2018  18:36
27    101   buy  7695.00  0.001058  23.07.2018  18:36
28    102   buy  7702.84  0.001058  23.07.2018  18:37
  

您可以使用shift从自身上方的行中获取一行的值并将其存储为col_2b

>>> df['col_2b'] = df['col_2'].shift(1)
>>> df
    col_1 col_2    col_3     col_4       col_5  col_6 col_2b
0      74   buy  7395.41  0.000022  19.07.2018  16:00    NaN
1      75  sell  7380.11  0.011296  20.07.2018   6:45    buy
2      76   buy  7503.56  0.011090  20.07.2018  14:30   sell
3      77  sell  7272.64  0.011091  21.07.2018   2:30    buy
4      78   buy  7363.81  0.010974  21.07.2018  14:00   sell
5      79  sell  7450.58  0.010974  22.07.2018  13:45    buy
6      80   buy  7497.86  0.010892  22.07.2018  17:30   sell
7      81  sell  7405.44  0.010892  22.07.2018  23:45    buy
8      82   buy  7605.28  0.010603  23.07.2018   2:30   sell
9      83  sell  7742.51  0.001060  23.07.2018  18:23    buy
10     84  sell  7743.68  0.001060  23.07.2018  18:24   sell
11     85  sell  7748.71  0.001060  23.07.2018  18:24   sell
12     86  sell  7751.81  0.001060  23.07.2018  18:25   sell
13     87  sell  7755.07  0.001060  23.07.2018  18:25   sell
14     88  sell  7758.39  0.001060  23.07.2018  18:26   sell
15     89  sell  7745.54  0.001060  23.07.2018  18:26   sell
16     90  sell  7748.11  0.001060  23.07.2018  18:27   sell
17     91  sell  7751.05  0.001060  23.07.2018  18:27   sell
18     92  sell  7748.11  0.001060  23.07.2018  18:28   sell
19     93   buy  7774.71  0.001058  23.07.2018  18:32   sell
20     94   buy  7779.51  0.001058  23.07.2018  18:33    buy
21     95   buy  7769.85  0.001058  23.07.2018  18:33    buy
22     96   buy  7748.35  0.001058  23.07.2018  18:34    buy
23     97   buy  7730.00  0.001058  23.07.2018  18:34    buy
24     98   buy  7649.00  0.001058  23.07.2018  18:35    buy
25     99   buy  7644.99  0.001058  23.07.2018  18:35    buy
26    100   buy  7696.30  0.001058  23.07.2018  18:36    buy
27    101   buy  7695.00  0.001058  23.07.2018  18:36    buy
28    102   buy  7702.84  0.001058  23.07.2018  18:37    buy
  

然后,只需过滤col_2col_2b中具有不同值的行:

>>> df = df[df['col_2b'] != df['col_2']]
>>> df
    col_1 col_2    col_3     col_4       col_5  col_6 col_2b
0      74   buy  7395.41  0.000022  19.07.2018  16:00    NaN
1      75  sell  7380.11  0.011296  20.07.2018   6:45    buy
2      76   buy  7503.56  0.011090  20.07.2018  14:30   sell
3      77  sell  7272.64  0.011091  21.07.2018   2:30    buy
4      78   buy  7363.81  0.010974  21.07.2018  14:00   sell
5      79  sell  7450.58  0.010974  22.07.2018  13:45    buy
6      80   buy  7497.86  0.010892  22.07.2018  17:30   sell
7      81  sell  7405.44  0.010892  22.07.2018  23:45    buy
8      82   buy  7605.28  0.010603  23.07.2018   2:30   sell
9      83  sell  7742.51  0.001060  23.07.2018  18:23    buy
19     93   buy  7774.71  0.001058  23.07.2018  18:32   sell
  

最后,删除col_2b

>>> del df['col_2b']
>>> df
    col_1 col_2    col_3     col_4       col_5  col_6
0      74   buy  7395.41  0.000022  19.07.2018  16:00
1      75  sell  7380.11  0.011296  20.07.2018   6:45
2      76   buy  7503.56  0.011090  20.07.2018  14:30
3      77  sell  7272.64  0.011091  21.07.2018   2:30
4      78   buy  7363.81  0.010974  21.07.2018  14:00
5      79  sell  7450.58  0.010974  22.07.2018  13:45
6      80   buy  7497.86  0.010892  22.07.2018  17:30
7      81  sell  7405.44  0.010892  22.07.2018  23:45
8      82   buy  7605.28  0.010603  23.07.2018   2:30
9      83  sell  7742.51  0.001060  23.07.2018  18:23
19     93   buy  7774.71  0.001058  23.07.2018  18:32

希望这会有所帮助!