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

470
Vistas
How to do rolling subtraction repeatedly with Pandas Dataframe

How to do rolling subtraction repeatedly with a Pandas Dataframe?

I have a dataframe that looks like this:

value
100
1
4
10
5
3

I want to repeatedly subtract previous value with current value. I want the following output:

100
99
95
85
80
77

How can I do this? If I have multiple columns in the dataframe, how can I apply the same operation to all of the columns?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

A vectorized solution:

-(df['value']).cumsum() + df['value'].iat[0]*2

Output:

0    100
1     99
2     95
3     85
4     80
5     77
Name: value, dtype: int64
over 4 years ago · Santiago Trujillo Denunciar

0

Combine expanding with np.subtract.reduce:

df.value.expanding(1).agg(np.subtract.reduce)

0    100.0
1     99.0
2     95.0
3     85.0
4     80.0
5     77.0
Name: value, dtype: float64
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