You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何删除Pandas中utime列值与前一行重复的行

解决方法

你可以使用pandas的drop_duplicates()方法,指定按utime列去重,并保留每组重复值的第一行,具体实现如下:

原数据

import pandas as pd

data = { 
    'utime': [1461098442,1461098443,1461098443,1461098444,1461098445],
    'lat': [41.1790265,41.1791703,41.1791464,41.1791703,41.1791419],
    'lon': [-8.5951883,-8.5951229,-8.5951376,-8.5951229,-8.5951365]
}

df = pd.DataFrame(data)
print(df)

输出:

utime        lat        lon
0  1461098442  41.179026  -8.595188
1  1461098443  41.179170  -8.595123
2  1461098443  41.179146  -8.595138
3  1461098444  41.179170  -8.595123
4  1461098445  41.179142  -8.595137

去重代码

# 按utime列去重,保留每组重复值的第一行
df_cleaned = df.drop_duplicates(subset='utime', keep='first')
print(df_cleaned)

输出:

utime        lat        lon
0  1461098442  41.179026  -8.595188
1  1461098443  41.179170  -8.595123
3  1461098444  41.179170  -8.595123
4  1461098445  41.179142  -8.595137

参数说明

  • subset='utime':指定以utime列作为判断重复值的依据
  • keep='first':保留每组重复值中最先出现的行,若需要保留最后一行可改为keep='last'

内容的提问来源于stack exchange,提问作者Amina Umar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:00:56