Pandas应用一个同时影响2列的更改

时间:2016-05-27 09:14:39

标签: python pandas

我有以下数据框。 bet_pl和co_pl跟踪2个余额的每日变化。我已根据co_pl和cumsum更新了co_balance。

init_balance = D('100.0')
co_thresh = D('1.05') * init_balance

def get_pl_co(row):
    if row['eod_bet_balance'] > co_thresh:
        diff = row['eod_bet_balance']- co_thresh
        return(diff)
    else:
        return Decimal('0.0') 


df_odds_winloss['eod_bet_balance'] = df_odds_winloss['bet_pl'].cumsum()+initial_balance
df_odds_winloss['sod_bet_balance']= df_odds_winloss['eod_bet_balance'].shift(1).fillna(init_balance)
df_odds_winloss['co_pl'] = df_odds_winloss.apply(get_pl_co, axis=1)
df_odds_winloss['co_balance'] = df_odds_winloss['co_pl'].cumsum()

# trying this
df_odds_winloss['eod_bet_balance'] = df_odds_winloss['eod_bet_balance'] - df_odds_winloss['co_pl'] 

现在我希望eod_bet_balance更新为负co_pl,因为它是两个余额之间的转移,但是没有得到正确的eod(一天结束)余额。

任何人都可以提示吗?

更新:eod_balances反映了bet_pl的变化,但没有反映co_pl的后续变化。

最终更新:

initial_balance = D('100.0')
df = pd.DataFrame({ 'SP': res_df['SP'], 'winloss': bin_seq_l}, columns=['SP', 'winloss'])
df['bet_pl'] = df.apply(get_pl_lvl, axis=1)
df['interim_balance'] = df_odds_winloss['bet_pl'].cumsum()+initial_balance
df['co_pl'] = (df['interim_balance'] - co_thresh).clip_lower(0)
df['co_balance'] = df_odds_winloss['co_pl'].cumsum()
df['post_co_balance'] = df['interim_balance'] - df['co_pl']
bf_r = D('0.05')
df['post_co_deduct_balance'] = df['post_co_balance'] - (df['post_co_balance']* bf_r)
df['sod_bet_balance'] = df['post_co_deduct_balance'].shift(1).fillna(init_balance) 

The final solution

1 个答案:

答案 0 :(得分:1)

首先,您不需要应用自定义函数来获取co_pl,可以这样做:

df['co_pl'] = (df['eod_bet_balance'] - co_thresh).clip_lower(0)

至于更新其他专栏,如果我理解正确你想要这样的东西:

df['eod_bet_balance'] = df['eod_bet_balance'].clip_upper(co_thresh)

或等同地......

df['eod_bet_balance'] -= df['co_pl']