简体   繁体   中英

pandas: delete a row having same value of a column as in previous raw

Here is my df:

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)
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

Two samples are received at same time (unix epoch 1461098443 ) so I want retain 1, and delete the other.

So that I have

       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

drop_duplicates should help (read https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop_duplicates.html )

df.drop_duplicates(subset='utime')
df = df.groupby('utime', as_index=False).agg('first')
        utime        lat       lon
0  1461098442  41.179026 -8.595188
1  1461098443  41.179170 -8.595123
2  1461098444  41.179170 -8.595123
3  1461098445  41.179142 -8.595137

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM