Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
How to apply lambda function to specific column based on the values in the adjacent column

I am trying a apply a lambda function to a pandas data frame. My question is how can I apply a lambda function to column a based on value in column b using if statement.

A B C
2 5 7
4 5 9
6 7 9
df['B'].apply(lambda x: x+3 if x<(#the value in column C) else x)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to call apply on the dataframe, with axis=1, instead of on the B column:

>>> df.apply(lambda x: x['B']+3 if x['B']<x['C'] else x['B'], axis=1)
0     8
1     8
2    10
dtype: int64

But, a much more efficient (faster) way would be to do this:

>>> df['B'] + df['B'].lt(df['C']) * 3
0     8
1     8
2    10
dtype: int64
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!