Python-部分重复的行-仅保留最后一个

时间:2018-09-14 13:11:30

标签: python pandas csv

我们有以下代码:

data = [['BQXBTC', '9/14/2018', '5:15:00', 4.792e-05, 4.8e-05, 4.777e-05, 4.783e-05, 30027.0], ['BQXBTC', '9/14/2018', '5:30:00', 4.79e-05, 4.817e-05, 4.78e-05, 4.811e-05, 10151.0], ['BQXBTC', '9/14/2018', '5:45:00', 4.788e-05, 4.811e-05, 4.764e-05, 4.767e-05, 9682.0], ['BQXBTC', '9/14/2018', '6:00:00', 4.766e-05, 4.796e-05, 4.759e-05, 4.761e-05, 22046.0], ['BQXBTC', '9/14/2018', '6:00:00', 4.766e-05, 4.796e-05, 4.759e-05, 4.761e-05, 22094.0], ['BQXBTC', '9/14/2018', '6:15:00', 4.761e-05, 4.77e-05, 4.761e-05, 4.763e-05, 26761.0], ['ETCBTC', '9/14/2018', '2:15:00', 0.001712, 0.001718, 0.001711, 0.001713, 9065.28], ['ETCBTC', '9/14/2018', '2:30:00', 0.001712, 0.001718, 0.001712, 0.001716, 11504.56], ['ETCBTC', '9/14/2018', '2:45:00', 0.001717, 0.001719, 0.00171, 0.001711, 10365.09], ['ETCBTC', '9/14/2018', '3:00:00', 0.001712, 0.001721, 0.001709, 0.001716, 8922.49], ['ETCBTC', '9/14/2018', '3:00:00', 0.001712, 0.001721, 0.001709, 0.001714, 8924.85], ['ETCBTC', '9/14/2018', '3:15:00', 0.001716, 0.001718, 0.001709, 0.00171, 14429.7]]
df = pd.DataFrame(data)
print(df)

返回此:

         0          1        2         3         4         5         6         7
0   BQXBTC  9/14/2018  5:15:00  0.000048  0.000048  0.000048  0.000048  30027.00
1   BQXBTC  9/14/2018  5:30:00  0.000048  0.000048  0.000048  0.000048  10151.00
2   BQXBTC  9/14/2018  5:45:00  0.000048  0.000048  0.000048  0.000048   9682.00
3   BQXBTC  9/14/2018  6:00:00  0.000048  0.000048  0.000048  0.000048  22046.00
4   BQXBTC  9/14/2018  6:00:00  0.000048  0.000048  0.000048  0.000048  22094.00
5   BQXBTC  9/14/2018  6:15:00  0.000048  0.000048  0.000048  0.000048  26761.00
6   ETCBTC  9/14/2018  2:15:00  0.001712  0.001718  0.001711  0.001713   9065.28
7   ETCBTC  9/14/2018  2:30:00  0.001712  0.001718  0.001712  0.001716  11504.56
8   ETCBTC  9/14/2018  2:45:00  0.001717  0.001719  0.001710  0.001711  10365.09
9   ETCBTC  9/14/2018  3:00:00  0.001712  0.001721  0.001709  0.001716   8922.49
10  ETCBTC  9/14/2018  3:00:00  0.001712  0.001721  0.001709  0.001714   8924.85
11  ETCBTC  9/14/2018  3:15:00  0.001716  0.001718  0.001709  0.001710  14429.70

问题是(部分)重复的行:

1)第3行和第4行位于该符号的同一日期和时间,但是音量(第7行)略有不同。

2)该符号的第9行和第10行位于相同的日期和时间,但是收盘价(第6行)和交易量(第7行)略有不同。

解决此问题的逻辑是:

如果有多个行具有相同的col 0(符号),col 1(日期)和col 2(时间), 仅计算最后一行,丢弃前一秒。

这是所需的输出:

         0          1        2         3         4         5         6         7
0   BQXBTC  9/14/2018  5:15:00  0.000048  0.000048  0.000048  0.000048  30027.00
1   BQXBTC  9/14/2018  5:30:00  0.000048  0.000048  0.000048  0.000048  10151.00
2   BQXBTC  9/14/2018  5:45:00  0.000048  0.000048  0.000048  0.000048   9682.00
3   BQXBTC  9/14/2018  6:00:00  0.000048  0.000048  0.000048  0.000048  22094.00
4   BQXBTC  9/14/2018  6:15:00  0.000048  0.000048  0.000048  0.000048  26761.00
5   ETCBTC  9/14/2018  2:15:00  0.001712  0.001718  0.001711  0.001713   9065.28
6   ETCBTC  9/14/2018  2:30:00  0.001712  0.001718  0.001712  0.001716  11504.56
7   ETCBTC  9/14/2018  2:45:00  0.001717  0.001719  0.001710  0.001711  10365.09
8   ETCBTC  9/14/2018  3:00:00  0.001712  0.001721  0.001709  0.001714   8924.85
9   ETCBTC  9/14/2018  3:15:00  0.001716  0.001718  0.001709  0.001710  14429.70

我们如何做到?

0 个答案:

没有答案