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



我们有这个代码:

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 行在该符号的日期和时间相同,但音量 (col 7( 略有不同。

2( 第 9 行和第 10 行在该交易品种的日期和时间相同,但收盘价 (列 6( 和成交量 (列 7( 略有不同。

解决此问题的逻辑是:

如果有多个行具有相同的列 0(符号(、列 1(日期(和列 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

我们怎么做?

你想要.drop_duplicates

df.drop_duplicates(subset=[0,1,2], keep='last', inplace=True)

相关内容

  • 没有找到相关文章

最新更新