简体   繁体   中英

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()."

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM