0

Good day,

I would like to ask if it's possible to access more than one column in a lambda-function inside a pandas-dataframe or if there's an alternative!?

For example my dataframe is looking something like this:

value_a  |  value_b  |  value_c
1        |  17       |  8
2        |  253      |  9
3        |  89       |  8
...

I also got a function that is calculating with some of the data:

def some_function(a, b):
    ...do something:
    return c

Now I want to use lambda-function to calculate together with the function but include the data from two columns. Something like this...

df['value_d'] = df['value_b'].apply(lambda x: some_function(x, df['value_c']))

Is it possible to access more than one column inside such a function or is there a better solution?

Hoping my question is understandable.

Thanks to all of you and have a great day!

0

1 Answer 1

4

use apply over whole df

df['value_d'] = df.apply(lambda row: some_function(row['value_b'],row['value_c']), 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.