【发布时间】:2018-03-03 03:43:02
【问题描述】:
我正在尝试确定索引中的哪些时间戳有重复项。我想创建一个时间戳字符串列表。如果可能,我想为每个重复的时间戳返回一个时间戳。
#required packages
import os
import pandas as pd
import numpy as np
import datetime
# create sample time series
header = ['A','B','C','D','E']
period = 5
cols = len(header)
dates = pd.date_range('1/1/2000', periods=period, freq='10min')
dates2 = pd.date_range('1/1/2022', periods=period, freq='10min')
df = pd.DataFrame(np.random.randn(period,cols),index=dates,columns=header)
df0 = pd.DataFrame(np.random.randn(period,cols),index=dates2,columns=header)
df1 = pd.concat([df]*3) #creates duplicate entries by copying the dataframe
df1 = pd.concat([df1, df0])
df2 = df1.sample(frac=1) #shuffles the dataframe
df3 = df1.sort_index() #sorts the dataframe by index
print(df2)
#print(df3)
# Identifying duplicated entries
df4 = df2.duplicated()
print(df4)
然后我想使用列表调出每个时间戳的所有重复条目。从上面的代码中,有没有一种好方法可以调用与为 false 的 bool 类型相关的索引?
编辑:添加了一个额外的数据框以创建一些唯一值,并将第一个数据框增加了三倍以创建多个重复。还为问题添加了更多细节。
【问题讨论】:
标签: python-3.x pandas dataframe duplicates timestamp