how can i simplify conditions in pandas?

Use numpy.select. It would be the fastest in performance.

import numpy as np

conditions = [(df.Y - df.Z) > df.X, (df.Y + df.Z) < df.X]
choices = [df.Y - df.Z, df.Y + df.Z]

df['A'] = np.select(conditions, choices, default=df.X)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top