0

My original code looks like this. I passed a value in the row to a function and replaced a value based on the result.

m = df["value_a"].isin( a_list)
df.loc[m, "value_b"] = df.loc[m, "value_a"].apply(lambda x: get_new_value_b(arg1, x))

And now I need to pass multiple values in a row to the function. When I pass the values like below, seems like the value is the entire column, not the value in a row.

df.loc[m, "value_b"] = df.loc[m, ["value_a", "value_c"]].apply(lambda x: get_new_value_b(arg1, x))

...

def get_new_value_b(arg1, x):
    print("x: {x}".format(x=x))
    ...


# x: 
# 0    ...
# 1    ...
# ...
# value_a, dtype: object

How can I pass two values (value_a and value_c) to the function properly here?

1
  • get_new_value_b(arg1, *x)? Commented Jul 25, 2022 at 7:57

1 Answer 1

1

You can use axis=1 of DataFrame.apply

df.loc[m, "value_b"] = df.loc[m, ["value_a", "value_c"]].apply(lambda row: get_new_value_b(arg1, row), axis=1)

Then access the row value in get_new_value_b with row['col']

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.