One Pandas Function That Can Save You From Ugly if/else Suppose you… — Data science/ML/AI — TG.ME

🐼 One Pandas Function That Can Save You From Ugly if/else

Suppose you want to classify customers:
spending >= 1000 → VIP
spending >= 500 → Regular
otherwise → Low


You could write a complicated function.
Or:
import numpy as np

df["segment"] = np.select(
[
df["spending"] >= 1000,
df["spending"] >= 500
],
[
"VIP",
"Regular"
],
default="Low"
)

Now the rules are visible directly in the code.

This becomes especially useful when you have several conditions.
❤2
August 30, 2026 339 1 2