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

Time:09-17

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

CodePudding user response:

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

df.drop_duplicates(subset='utime')

CodePudding user response:

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
  • Related