Skip to content Skip to sidebar Skip to footer

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? df

Solution 1:

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)

Post a Comment for "Faster Way To Choose Minimum Of Two Column If Condition Meets Else Just One"