Home > Blockchain >  Dataframe column: to find (cumulative) local maxima
Dataframe column: to find (cumulative) local maxima

Time:05-01

In the below dataframe the column "CumRetperTrade" is a column which consists of a few vertical vectors (=sequences of numbers) separated by zeros. (= these vectors correspond to non-zero elements of column "Portfolio"). I would like to find the cumulative local maxima of every non-zero vector contained in column "CumRetperTrade". To be precise, I would like to transform (using vectorization - or other - methods) column "CumRetperTrade" to the column "PeakCumRet" (desired result) which gives for every vector ( = subset corresponding to ’Portfolio =1 ’) contained in column "CumRetperTrade" the cumulative maximum value of (all its previous) values. The numeric example is below. Thanks in advance! PS In other words, I guess that we need to use cummax() but to apply it only to the consequent (where 'Portfolio' = 1) subsets of 'CumRetperTrade'

import numpy as np
import pandas as pd
df1 = pd.DataFrame({"Portfolio": [1, 1, 1, 1, 0 , 0, 0, 1, 1, 1],
"CumRetperTrade": [2, 3, 2, 1, 0 , 0, 0, 4, 2, 1],
"PeakCumRet": [2, 3, 3, 3, 0 , 0, 0, 4, 4, 4]})
df1

    Portfolio   CumRetperTrade  PeakCumRet
0   1           2               2
1   1           3               3
2   1           2               3
3   1           1               3
4   0           0               0
5   0           0               0
6   0           0               0
7   1           4               4
8   1           2               4
9   1           1               4


PPS I already asked a similar question previously (Dataframe column: to find local maxima) and received a correct answer to my question, however in my question I did not explicitly mention the requirement of cumulative local maxima

CodePudding user response:

You only need a small modification to the previous answer:

df1["PeakCumRet"] = (
    df1.groupby(df1["Portfolio"].diff().ne(0).cumsum())
    ["CumRetperTrade"].expanding().max()
    .droplevel(0)
)

expanding().max() is what produces the local maxima.

  • Related