计算不存储在DataFrame中,但适用于打印

时间:2016-08-18 08:37:49

标签: python pandas numpy

我有以下计算:
np.maximum(0, np.prod([perf_asset, calc_arr['val']]) - amt_payout - np.prod([exposure, calc_arr['delta_1']]))

写出来,这将是:

MAX(0, 0.8 × 105.015038 - 80 - TRUE × 5.3135)
MAX(0, 84.0120307692 - 80 - 5.3135)
= 0

如果我打印这个,输出实际上有效但如果我想将它存储在DataFrame中,它不会: calc_arr['added_amt'] = np.maximum(0, np.prod([perf_asset, calc_arr['val']]) - amt_payout - np.prod([exposure, calc_arr['delta_1']]))

计算突然停止了。在此之前,我甚至不必使用np.prodnp.sum。说实话我很困惑。

完成循环:

j = 1
for i in [0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,-0.053,-0.0698,-0.1011,-0.1767,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271]:
    risky_return = i
    risk_free_return = (-0.0125/260)

    stock_val = stock_calc(j, stock_val['amt_payout'], stock_val['alloc_risky'], stock_val['alloc_risk_free'], stock_val['delta_1'], risky_return, risk_free_return)
    stock_vals = stock_vals.append(stock_val)
    j = j + 1

检索calc_arr['val']

calc_arr['val'] = np.sum([np.prod([(1 + perf_risky), alloc_risky]), np.prod([(1 + perf_risk_free), alloc_risk_free])])

1 个答案:

答案 0 :(得分:0)

我认为您需要append个值list - stock_vals,然后将其分配到列:

stock_vals = []
for i in [0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,-0.053,-0.0698,-0.1011,-0.1767,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271]:
    risky_return = i
    risk_free_return = (-0.0125/260)

    stock_val = stock_calc(j, stock_val['amt_payout'], stock_val['alloc_risky'], stock_val['alloc_risk_free'], stock_val['delta_1'], risky_return, risk_free_return)
    stock_vals.append(stock_val)


calc_arr['val'] = stock_vals

我尝试重写列表理解:

L = [0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,0.0627,-0.053,-0.0698,-0.1011,-0.1767,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271,-0.6271]

calc_arr['val'] = [stock_vals.append(stock_calc(j, stock_val['amt_payout'], stock_val['alloc_risky'], stock_val['alloc_risk_free'], stock_val['delta_1'], risky_return, (-0.0125/260))) for risky_return in L]