Home > Software design >  Drop the row of dataframe if it's duplicate and if yes drop all except with minimum time
Drop the row of dataframe if it's duplicate and if yes drop all except with minimum time

Time:01-03

I have two columns "key" and "time". If the key is same then compare time, keep the minimum time row and drop other with that particular key.

import pandas as pd

data = {
  "key": [1,2,3,4,1,2,3,1],
  "time": [12.4,12.6,12.8,12.5,12.9,12.3,12.8,12.1],
}

df = pd.DataFrame(data)

print(df)

I tried with duplicate() but it returns series. Tried many other things but didn't work.

CodePudding user response:

You can use groupby over key and aggregate by minimum as in:

df.groupby(['key']).agg('min')

  • Related