change value in a column with apply lambda

Do you have a trick to avoid that the following code changes the cells (and keep the existing data) when the condition in the lambda function is not met

df['test'] = df['Q8_3'].apply(lambda x: 'serial' if x >=3 else 'toto')

This code is running when I add ‘toto’ after the else statement but I would like to bypass the else statement. Thanks for your help


is this helps you?

df = pd.DataFrame({
    'Q8_3': [1, 2, 3, 4, 5],
    'test': ['old','old','old','old','old']

df.loc[df.Q8_3 >3 , 'test'] = "serial"


    Q8_3    test
0     1     old
1     2     old
2     3     old
3     4  serial
4     5  serial