简体   繁体   English

使用 Pandas 应用函数时使用前几行的值

[英]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.嗨,我正在尝试为时间序列 Pandas 数据框创建新列,该数据框本质上是跟踪电池的充电和放电。 I can make it work with iterrows, but as you might expect it's very slow on a large time-series.我可以让它与 iterrows 一起工作,但正如你所料,它在大时间序列上非常慢。 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.使用 pd.shift(1) 获取顶部的最后一个值。 Use pd.shift(-1) to get the next row below.使用 pd.shift(-1) 获取下面的下一行。 Use np.where similar to =IF function in excel.在 excel 中使用类似于 =IF 函数的 np.where。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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