Python知道如何随时间删除重复项

Python知道如何随时间删除重复项,python,pandas,dataframe,Python,Pandas,Dataframe,例如: df 值日期\时间 1 2020.12.1 1 2020.12.2您可以尝试: df[df['value'].ne(df['value'].shift())] 输出: value date_time 0 1 2020.12.1 3 2 2020.12.4 5 1 2020.12.6 您可以尝试: df[df['value'].ne(df['value'].shift())] 输出: value date_time 0

例如: df

值日期\时间
1     2020.12.1
1 2020.12.2您可以尝试:

df[df['value'].ne(df['value'].shift())]
输出:

   value  date_time
0      1  2020.12.1
3      2  2020.12.4
5      1  2020.12.6
您可以尝试:

df[df['value'].ne(df['value'].shift())]
输出:

   value  date_time
0      1  2020.12.1
3      2  2020.12.4
5      1  2020.12.6
用移位值替换Comapre by并传递到:

如果要测试多个列的重复项,请使用类似的解决方案测试所有列并添加:

用移位值替换Comapre by并传递到:

如果要测试多个列的重复项,请使用类似的解决方案测试所有列并添加:


如果我有两列['type'、'value'、'date\u time'],我想将它们组合起来检查是否重复。@Niuya-答案已编辑。在您的示例中,我想使用type&value作为不同的键。所以我想删除索引为4的行,因为它们与索引3完全相同。所以可能只是将cols保持为['type','value']?@Niuya-确切地说,需要
cols=['type','value']
如果我有两列['type','value','date\u time'],我想组合它们来检查它是否重复。@Niuya-答案被编辑了。在你的例子中,我想使用type和value作为不同的键。所以我想删除索引为4的行,因为它们与索引3完全相同。所以可能只是将cols保持为['type','value']?@Niuya-确切地说,需要
cols=['type','value']
print (df)
   value  date_time type
0      1  2020.12.1    a
1      1  2020.12.1    a <- duplicate by 3 columns, removed
2      1  2020.12.1    b
3      2  2020.12.4    b
4      2  2000.12.4    b
5      1  2020.12.4    b

cols = ['type','value','date_time']
df2 = df[df[cols].ne(df[cols].shift()).any(axis=1)]

print (df2)
   value  date_time type
0      1  2020.12.1    a
2      1  2020.12.1    b
3      2  2020.12.4    b
4      2  2000.12.4    b
5      1  2020.12.4    b
cols = ['type','value']
df3 = df[df[cols].ne(df[cols].shift()).any(axis=1)]

print (df3)
   value  date_time type
0      1  2020.12.1    a
2      1  2020.12.1    b
3      2  2020.12.4    b
5      1  2020.12.4    b