Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

473
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda