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

225
Visualizações
Conditional counting in pandas df

I have a dataframe of stock prices:

df = pd.DataFrame([100, 101, 99, 100,105,104,106], columns=['P'])

I would like to create a counter column, that counts either if the current price is higher than the previous row's price, BUT if the current price is lower than the previous row's price, only counts again, once that price is exceeded (like a watermark). Below is the desired column:

df['counter'] = [np.nan, 1, 1, 1,2,2,3]

So the second row's price is 101 which exceeds 100, so the counter is 1, then the price drops to 99 and comes back to 100, but the counter is still 1, because we have not reached the 101 price (which is the watermark), then once we exceed 101 in row 4, with a price of 105, the counter goes to 2, then the price drops to 104 again, so we stay at 2, and then when it goes to 106 we increase the counter to 3.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Algorithm:

  1. Find what current maximum previously observed value was at each row (inclusive of the current row).

  2. See what the maximum previously observed value was for the preceding row.

  3. Each time a difference exists between these two values, we know that a new water mark has been hit within the current row.

  4. Calculate the cumulative sum of the number of times a new water mark has been hit.

     df["current_observed_max"] = df["p"].cummax()
     df["previous_observed_max"] = df["current_observed_max"].shift(1)
     df["is_new_watermark"] =(df["current_observed_max"] != df["previous_observed_max"]).astype(int)
     df["counter"] = df["is_new_watermark"].cumsum()
    

With this you may need to subtract 1 depending on how you would like to handle the first observed number.

over 4 years ago · Santiago Trujillo Relatório

0

A very simple and efficient method is to combine pandas.factorize and cummax:

df['counter'] = pd.factorize(df['P'].cummax())[0]

Output:

     P  counter
0  100        0
1  101        1
2   99        1
3  100        1
4  105        2
5  104        2
6  106        3
over 4 years ago · Santiago Trujillo Relatório

0

Another way: Find if the row value is equal to the cummulative maximum and cumsum() to create unique groups

df['newP']=(df['P'].cummax()==df['P']).cumsum()-1

     P  newP
0  100     0
1  101     1
2   99     1
3  100     1
4  105     2
5  104     2
6  106     3
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