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)