mercredi 8 août 2018

lambda function of if else statement in python pandas [duplicate]

This question already has an answer here:

I would like to overwrite a variable according to a specific condition and in the other cases keep its original value.

in pseudo code: if age < 2 then id=99, else age

I miss the "else age" style sentence

raw_data1 = {'id': [1,2,3,5],
    'age': [0, np.nan, 10, 2]}
df1 = pd.DataFrame(raw_data1, columns = ['id','age'])



df1['id'] = np.where(df1['age']<2,99)

df1['id']=df['age'].apply(lambda x:99 if x<2)

Aucun commentaire:

Enregistrer un commentaire