I need to create a column with the max of the value in column A and the value of the row before of column B. This will provide a list of max values which is updated over the time.
I was thinking writing this code:
SPY['Peak Equity']=SPY['Close price']-SPY['Peak Equity'].shift(-1)
but it doesn't work.
Could you please help me? Many thanks in advance!
Yes I'm a beginner ... :-)
CodePudding user response:
I think expanding
is what you are looking for.
SPY['Peak Equity'] = SPY['Close Price'].expanding(1).max()
You can see detail of expanding
function here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html
CodePudding user response:
Your question is a bit unclear, but it seems like you're looking for .cummax()
:
With a df
like
df = pd.DataFrame({"A": [2, 1, 2, 3, 1, 5, 4, 7]})
A
0 2
1 1
2 2
3 3
4 1
5 5
6 4
7 7
this
df["B"] = df["A"].cummax()
gives you
A B
0 2 2
1 1 2
2 2 2
3 3 3
4 1 3
5 5 5
6 4 5
7 7 7