Drop duplicates with condition

I have the following pandas dataframe:

index so_id
10 390
10 395
10 405
11 390
11 395
11 405
12 390
12 395
12 405

The desired output would be the following:

index so_id
10 390
11 395
12 405

Basically my goal is to drop duplicates on the column ‘index’ while keeping a different ascending value for the column ‘so_id’

Answer

We can do it but importantly, pay attention to comment above.

df=df.sort_values (by=['so_id'])#Sort df

Create temporary column t which is a classification of so_id and resort df back to original df=df.assign(t=df['so_id'].ne(df['so_id'].shift(1)).cumsum()).sort_values(by='index')

Create a temporary classification of index

df=df.assign(t1=df['index'].ne(df['index'].shift(1)).cumsum())

Select where two classess above are similar

df=df[df['t']==df['t1']].drop(columns=['t','t1'])

print(df)

    index  so_id
0     10    390
4     11    395
8     12    405