Mik*_*e R 3 python duplicates dataframe pandas
我有一个像这样的数据帧:
Subject Verb Object Date
---------------------------------
Bill Ate Food 7/11/2015
Steve Painted House 8/12/2011
Bill Ate Food 7/13/2015
Steve Painted House 8/25/2011
Run Code Online (Sandbox Code Playgroud)
我想删除所有重复项,其中重复项被定义为具有相同的主语、动词、宾语,并且在 X 天范围内(在我的示例中:5 天)。
Subject Verb Object Date
---------------------------------
Bill Ate Food 7/11/2015
Steve Painted House 8/12/2011
Steve Painted House 8/25/2011
Run Code Online (Sandbox Code Playgroud)
“Steve - Painted - House”的两个实例都没有被移除,因为它们在 5 天的窗口之外。
我知道我可以使用一些数据结构和 DataFrame 的 iterrows 方法来做到这一点,但是有没有办法使用 Pandas drop_duplicates 做到这一点?
结合使用duplicated+来确定要删除的行。diffgroupby
c = ['Subject', 'Verb', 'Object']
def f(x):
return x[c].duplicated() & x.Date.diff().dt.days.lt(5)
df = df.sort_values(c)
df[~df.groupby(c).apply(f).values]
Subject Verb Object Date
0 Bill Ate Food 2015-07-11
1 Steve Painted House 2011-08-12
3 Steve Painted House 2011-08-25
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1541 次 |
| 最近记录: |