Mahdi Abadinia
Mahdi Abadinia

Reputation: 75

Faster way to choose minimum of two column if condition meets else just one

I need to choose minimum of 'Start_x' and 'Start_y' if ['Overlap']=='Overlapped' otherwise just 'Start_x'. I wrote below but it is a bit slow. is there a faster way to do this?

df3['Start'] = df3[['Start_x','Start_y','Overlap']].apply
(
lambda x:x[['Start_x','Start_y']].min() if x['Overlap']=='Overlapped' 
else x['Start_x'], axis=1
)

Upvotes: 1

Views: 73

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Try via np.where():

import numpy as np
df['Start_x']=np.where(df['Overlap'].eq('Overlapped'),df[['Start_x','Start_y']].min(axis=1),df['Start_x'])

OR

via loc and boolean masking

df.loc[df['Overlap'].eq('Overlapped'),'Start_x']=df[['Start_x','Start_y']].min(axis=1)

Upvotes: 2

Related Questions