Skip to content
Advertisement

Alternative to apply function in pandas

I would like to execute this simple transformation in a more efficient way.

df["amount"] = df.apply(
    lambda row: 500 if row.amount > 500 else row.amount, axis=1
)

Any ideas?

Advertisement

Answer

You can use pandas.Series.clip:

df["amount"].clip(upper = 500)

or numpy.clip:

np.clip(df["amount"], None, 500)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement