简体   繁体   中英

Python: How to populate Pandas column that depends on the previous value (previous row)?

I am building a financial app. My position depends on the previous position (previous row) and also on the 'signal' column (same row) .

The DataFrame is called SPY.

position_arr = []
position = 0
for row in SPY['signal']:
    if row=='BUY' and position == 0:
        position = 1
    elif row=='SELL' and position == 0:
        position = -1
    elif row=='CLOSE SELL' and position == -1:
        position = 0
    elif row=='CLOSE BUY' and position == 1:
        position = 0
    position_arr.append(position)

SPY['position']=position_arr

Is there a better and more efficient way to do this?

You could shift the position column and use apply() on the column axis:

def apply_func(row):
    if row['signal']=='BUY' and row['pos_shifted'] == 0:
        position = 1
    elif row['signal']=='SELL' and row['pos_shifted'] == 0:
        position = -1
    elif row['signal']=='CLOSE SELL' and row['pos_shifted'] == -1:
        position = 0
    elif row['signal']=='CLOSE BUY' and row['pos_shifted'] == 1:
        position = 0
    return position

SPY['pos_shift'] = SPY['position'].shift()
SPY['position'] = SPY.apply(apply_func, axis=1)

You can use apply on 'signal' column and use a variable to hold the previous value.

prev_val = None  #if you don't know what should be the first value 

def check_condition(current_val):
    global prev_val
    val = 0
    if prev_val is not None:
        if current_val == 'BUY' and prev_val == 0:
            val = 1
        elif current_val == 'SELL' and prev_val == 0:
            val = -1
        elif current_val == 'CLOSE SELL' and prev_val == -1:
            val = 0
        elif current_val == 'CLOSE BUY' and prev_val == 1:
            val = 0
    else:  # handle the first row case separately
        val = 0  # TODO: what is the value for the first row?
    prev_val = val
    return val

df['position'] = df['signal'].apply(check_condition)

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