Filtering Close‑Timestamp Records in Pandas: Keep Only the First Entry
This article explains how to use Python's pandas library to group a dataset by multiple fields, sort by end timestamps, and retain only the first record when consecutive timestamps are within 20 seconds, providing a reusable code snippet and sample output.
1. Introduction
In a work scenario a data table contains columns "ID", "Stage", "Reviewer", "Amount", and "EndTime". The requirement is to group rows by the first four columns, sort each group by EndTime, and keep only the first record when consecutive EndTime values differ by 20 seconds or less.
2. Implementation
The solution uses pandas. A helper function func iterates over the sorted EndTime series, yields True for the first record of a group and for records whose time gap exceeds 20 seconds, otherwise yields False. The DataFrame is sorted, grouped, and filtered with transform(func).
import pandas as pd
def func(date_s):
"""filter function"""
min_date = date_s.iloc[0]
for num, i in enumerate(date_s):
if num and (i - min_date).seconds <= 20:
yield False
else:
min_date = i
yield True
df = pd.read_excel('工作量计算.xlsx')
df.sort_values(["编号", "环节", "审核人", "金额", "结束时间"], inplace=True)
df = df[df.groupby(["编号", "环节", "审核人", "金额"])['结束时间'].transform(func)]
print(df)The resulting DataFrame contains only the first record of each group when the time difference is within 20 seconds, matching the expected behavior.
3. Conclusion
The article demonstrates a practical pandas approach to de‑duplicate rows based on a 20‑second time window within each group, providing a reusable pattern for similar data‑cleaning tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
