5

Could anyone suggest a way answer the same question (see link) but by using lambda function: Update a dataframe in pandas while iterating row by row

1 Answer 1

13

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.