Sten
Sten

Reputation: 91

Pandas: Create a new column in a data frame with values calculated from an already existing column, i. calculate maximum

I want to create a new column with a max values calculation on the first column, as follows:

   High    Highest2P Highest3P  
0 101.0   102.0     103.0  
1 102.0   103.0     109.0  
2 103.0   109.0     109.0      
3 109.0   109.0  
4 100.0 

from pandas import *  

df = pd.DataFrame({  
    "High": pd.Series( [101.0, 102.0, 103.0, 109.0, 100.0] )  
})  

def calcHighest2P(x): return max(df["High"], df["High"].shift(-1))
def calcHighest3P(x): return max(df["High"], df["High"].shift(-1), df["High"].shift(-2))

df["Highest2P"] = calcHighest2P(df["High"])
df["Highest3P"] = calcHighest3P(df["High"])

But I get the following error message: "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

Upvotes: 1

Views: 87

Answers (1)

cs95
cs95

Reputation: 402333

You can use Rolling.max with assign:

df.assign(**{
    f'Highest{i}P': pd.Series(df.High.rolling(i).max().dropna().values) 
    for i in range(2, 4)}
)

    High  Highest2P  Highest3P
0  101.0      102.0      103.0
1  102.0      103.0      109.0
2  103.0      109.0      109.0
3  109.0      109.0        NaN
4  100.0        NaN        NaN

Upvotes: 1

Related Questions