【问题标题】:Create a list of duplicate index entries in pandas dataframe在 pandas 数据框中创建重复索引条目的列表
【发布时间】: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


    【解决方案1】:

    IIUC:

    df4[~df4]
    

    输出:

    2000-01-01 00:10:00    False
    2000-01-01 00:00:00    False
    2000-01-01 00:40:00    False
    2000-01-01 00:30:00    False
    2000-01-01 00:20:00    False
    dtype: bool
    

    时间戳列表,

    df4[~df4].index.tolist()
    

    输出:

    [Timestamp('2000-01-01 00:10:00'),
     Timestamp('2000-01-01 00:00:00'),
     Timestamp('2000-01-01 00:40:00'),
     Timestamp('2000-01-01 00:30:00'),
     Timestamp('2000-01-01 00:20:00')]
    

    【讨论】:

      【解决方案2】:
      In [46]: df2.drop_duplicates()
      Out[46]:
                                  A         B         C         D         E
      2000-01-01 00:00:00  0.932587 -1.508587 -0.385396 -0.692379  2.083672
      2000-01-01 00:40:00  0.237324 -0.321555 -0.448842 -0.983459  0.834747
      2000-01-01 00:20:00  1.624815 -0.571193  1.951832 -0.642217  1.744168
      2000-01-01 00:30:00  0.079106 -1.290473  2.635966  1.390648  0.206017
      2000-01-01 00:10:00  0.760976  0.643825 -1.855477 -1.172241  0.532051
      
      In [47]: df2.drop_duplicates().index.tolist()
      Out[47]:
      [Timestamp('2000-01-01 00:00:00'),
       Timestamp('2000-01-01 00:40:00'),
       Timestamp('2000-01-01 00:20:00'),
       Timestamp('2000-01-01 00:30:00'),
       Timestamp('2000-01-01 00:10:00')]
      

      【讨论】:

      • 这非常有效,并且比其他任何答案都更灵活。有没有一种简单的方法可以将时间戳列表转换为字符串?我尝试使用 to_string,但该列表没有该属性。基本上只是将时间戳列出为: ['2000-01-01 00:00:00' '2000-01-01 00:40:00' '2000-01-01 00:20:00' '2000- 01-01 00:30:00' '2000-01-01 00:10:00']
      猜你喜欢
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      相关资源
      最近更新 更多