I want to add new categorical features based on some conditions.
If we have only two conditions, let's say we want to check if variable is positive or not we can simply:
df.my_var.apply(lambda x: 'positive' if x>0 else 'negative')
How we can profit Python dict syntax when we have more that two cases, something like:
df.my_var.apply(lambda x: {x<0: 'negative, 0<x<1e+10: 'good', 1e+10<x: 'too_much'}.get(x))
Aucun commentaire:
Enregistrer un commentaire