Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda