[英]How do I drop duplicates and keep the last timestamp on pandas
我想刪除重復項並保留最后一個時間戳。 要刪除的重復項是customer_id
和var_name
是我的數據
customer_id value var_name timestamp
1 1 apple 2018-03-22 00:00:00.000
2 3 apple 2018-03-23 08:00:00.000
2 4 apple 2018-03-24 08:00:00.000
1 1 orange 2018-03-22 08:00:00.000
2 3 orange 2018-03-24 08:00:00.000
2 5 orange 2018-03-23 08:00:00.000
所以結果將是
customer_id value var_name timestamp
1 1 apple 2018-03-22 00:00:00.000
2 4 apple 2018-03-24 08:00:00.000
1 1 orange 2018-03-22 08:00:00.000
2 3 orange 2018-03-24 08:00:00.000
我認為需要使用sort_values
進行drop_duplicates
:
df = df.sort_values('timestamp').drop_duplicates(['customer_id','var_name'], keep='last')
print (df)
customer_id value var_name timestamp
0 1 1 apple 2018-03-22 00:00:00.000
3 1 1 orange 2018-03-22 08:00:00.000
2 2 4 apple 2018-03-24 08:00:00.000
4 2 3 orange 2018-03-24 08:00:00.000
如果不需要排序 - 訂單很重要:
df = df.loc[df.groupby(['customer_id','var_name'], sort=False)['timestamp'].idxmax()]
print (df)
customer_id value var_name timestamp
0 1 1 apple 2018-03-22 00:00:00
2 2 4 apple 2018-03-24 08:00:00
3 1 1 orange 2018-03-22 08:00:00
4 2 3 orange 2018-03-24 08:00:00
非常感謝您的解決方案。 僅供參考,第二個解決方案有點慢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.