我的日期如下,日期为索引。我想删除重复的日期
Stock Open High Low Close Adj Close Volume
Date
2016-05-13 AAD 5.230000 5.260000 5.200000 5.260000 5.260000 5000
2016-05-16 AAD 5.220000 5.260000 5.220000 5.260000 5.260000 6000
2016-05-17 AAD 5.210000 5.260000 5.210000 5.260000 5.260000 2000
2016-05-17 AAD 5.210000 5.260000 5.210000 5.260000 5.260000 2000
2016-05-18 AAD 5.200000 5.250000 5.200000 5.250000 5.250000 3000
我需要的输出
Stock Open High Low Close Adj Close Volume
Date
2016-05-13 AAD 5.230000 5.260000 5.200000 5.260000 5.260000 5000
2016-05-16 AAD 5.220000 5.260000 5.220000 5.260000 5.260000 6000
2016-05-17 AAD 5.210000 5.260000 5.210000 5.260000 5.260000 2000
2016-05-18 AAD 5.200000 5.250000 5.200000 5.250000 5.250000 3000
我尝试使用df.drop_duplicates()
,输出在重复日期之后删除额外的行。
Stock Open High Low Close Adj Close Volume
Date
2016-05-13 AAD 5.230000 5.260000 5.200000 5.260000 5.260000 5000
2016-05-16 AAD 5.220000 5.260000 5.220000 5.260000 5.260000 6000
2016-05-17 AAD 5.210000 5.260000 5.210000 5.260000 5.260000 2000
答案 0 :(得分:1)
让我们使用Jezrael提供的信息。
输入数据框:
print(df)
Stock Open High Low Close Adj Close Volume
2016-05-13 AAD 5.23 5.26 5.20 5.26 5.26 5000
2016-05-16 AAD 5.22 5.26 5.22 5.26 5.26 6000
2016-05-17 AAD 5.21 5.26 5.21 5.26 5.26 2000
2016-05-17 AAD 5.21 5.26 5.21 5.26 5.26 2000
2016-05-18 AAD 5.20 5.25 5.20 5.25 5.25 3000
df1 = df[~df.index.duplicated(keep='last')]
print(df1)
输出:
Stock Open High Low Close Adj Close Volume
2016-05-13 AAD 5.23 5.26 5.20 5.26 5.26 5000
2016-05-16 AAD 5.22 5.26 5.22 5.26 5.26 6000
2016-05-17 AAD 5.21 5.26 5.21 5.26 5.26 2000
2016-05-18 AAD 5.20 5.25 5.20 5.25 5.25 3000