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

Question:

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
Asked By: Amina Umar

||

Answers:

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
Answered By: Алексей Р

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

df.drop_duplicates(subset='utime')
Answered By: band
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.