vendredi 17 août 2018

None in if condition, how to handle missing data

If the value of "age" is missing I want to create a variable with the value of 1. instead everything is None in the output of the variable "Value".

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


def my_test(b):
    if b is None:
        return 1


df1['Value'] = df1.apply(lambda row: my_test(row['age']), axis=1)  

How can implement it?

Aucun commentaire:

Enregistrer un commentaire