1 回答
TA贡献1864条经验 获得超2个赞
我使用您在此处提供的有限数据创建了它。在使用它之前将所有空单元格设为 nan。
import pandas as pd
import numpy as np
v=[["1/5/2018",15.85 , 1, 74 , 1172.9],
["1/8/2018",16.50 ,0 ,np.nan,np.nan],
["2/14/2018" , 16.49, 0 ,np.nan,np.nan] ,
["2/15/2018", 16.03, -1 ,np.nan,np.nan]]
df=pd.DataFrame(v,columns=["Date","Close","Filter","O_Size","O_Tot"])
tmp = df[df.Filter==-1]
def func(x):
spl = df.iloc[:x.name,:]
val = spl.iloc[spl["O_Size"].last_valid_index()].O_Size*x.Close
return val
tmp["C_Tot"] = tmp.apply(func,axis=1)
res = pd.concat([df,tmp["C_Tot"]],axis=1)
输出
Date Close Filter O_Size O_Tot C_Tot
0 1/5/2018 15.85 1 74.0 1172.9 NaN
1 1/8/2018 16.50 0 NaN NaN NaN
2 2/14/2018 16.49 0 NaN NaN NaN
3 2/15/2018 16.03 -1 NaN NaN 1186.22
添加回答
举报