简体   繁体   中英

Using values from previous rows when using Pandas Apply Function

Hi I'm trying to create new columns to a time-series Pandas dataframe that is essentially tracking the charging and discharging of a battery. I can make it work with iterrows, but as you might expect it's very slow on a large time-series. From some internet searching im thinking Apply is the way to go (or not, im hoping you'll point me in the right direction) but im having trouble trying to access values from the previous time step. ive created this very simplified piece of code that tries to capture what im attempting to do. Basically i cannot figure out how to pass the 'end' value that i calculate on the previous row to the 'start' value on the next row.

df = pd.DataFrame(data = {'NetPosition': [-10, -5, 10], 'row_no': [0,1,2]})

df['start'] = 0
df['end'] = 0
df['dispatch'] = 0

starting_value = 20
max_rating = 4

def f(x):
    
    prev_index = max(0,int(x.row_no-1))
    
    if x.row_no == 0:
        start = starting_value
    else:
        start = df['end'].iloc[prev_index]
#         this is the part that doesn't work - im attempting to pull the end value from the previous row into the new next row

    if x['NetPosition']<0:
        
        dispatch = min(np.abs(x['NetPosition']), max_rating, start)
       
        end = start - dispatch
               
    else:
        dispatch = 0
        end = start
        
    return pd.Series([start,end,dispatch])


df[['start','end','dispatch']] = df.apply(lambda x: f(x), axis=1)

df

Use pd.shift(1) to get the last value on top. Use pd.shift(-1) to get the next row below. Use np.where similar to =IF function in excel.

import pandas as pd
import numpy as np

df = pd.DataFrame(data = {'NetPosition': [-10, -5, 10], 'row_no': [0,1,2]})

df['start'] = 0
df['end'] = 0
df['dispatch'] = 0

starting_value = 20
max_rating = 4

#Answer
df.dispatch = np.where(df.NetPosition < 0, min(max_rating,df['NetPosition'].abs().min()) ,0)
df.start = df.end.shift(1)
df.start = df.start.fillna(20)
df.end = np.where(df.NetPosition < 0, df.start, df.start - df.dispatch)
df

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