python - Conditional Substitution of values in pandas dataframe columns -


suppose i've pandas dataframe column values age df.age = {25, 35, 76, 21, 23, 30}

i want inplace replace this:

if df.age >=25 , df.age <= 35: replace value 1 else: replace value 0

i've tried df[df.age >= 7.35 , df.age <= 7.45, 'age'] = 0 doesn't seem work.

you can create function check conditions, , apply dataframe:

def condition(value):     if 25 <= value <= 35:         return 1     return 0  # stealing sample @anandskumar because i'm lazy in [32]: df out[32]:     age 0   25 1   35 2   76 3   21 4   23 5   30  in [33]: df['age'] = df['age'].apply(condition)  in [34]: df out[34]:     age 0    1 1    1 2    0 3    0 4    0 5    1 

or using 1 liner lambda:

df['age'] = df['age'].apply(lambda x: 1 if 25 <=  x <= 35 else 0) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -