Using Lambda Function Pandas to Set Column Values

16,209

You'll want to use apply with the parameter axis=1 to insure the function passed to apply is applied to each row.

The referenced question has an answer that uses this loop.

for i, row in df.iterrows():
    if <something>:
        row['ifor'] = x
    else:
        row['ifor'] = y

    df.ix[i]['ifor'] = x

To use a lambda with the same logic

df['ifor'] = df.apply(lambda row: x if something else y, axis=1)
Share:
16,209
MarcCharbo
Author by

MarcCharbo

Updated on June 09, 2022

Comments