我的问题我有一个循环,它使用基于其他列的值的公式或列中的前一个值创建列,具体取决于条件(“从新低开始的天数 == 0”)。在庞大的数据集上它真的很慢,所以我想摆脱循环并找到一个更快的公式。当前工作代码import numpy as npimport pandas as pdcsv1 = pd.read_csv('stock_price.csv', delimiter = ',')df = pd.DataFrame(csv1)for x in range(1,len(df.index)): if df["days from new low"].iloc[x] == 0: df["mB"].iloc[x] = (df["RSI on new low"].iloc[x-1] - df["RSI on new low"].iloc[x]) / -df["days from new low"].iloc[x-1] else: df["mB"].iloc[x] = df["mB"].iloc[x-1]df输入数据和预期输出RSI on new low,days from new low,mB0,22,029.6,0,1.329.6,1,1.329.6,2,1.329.6,3,1.329.6,4,1.321.7,0,-2.021.7,1,-2.021.7,2,-2.021.7,3,-2.021.7,4,-2.021.7,5,-2.021.7,6,-2.021.7,7,-2.021.7,8,-2.021.7,9,-2.025.9,0,0.525.9,1,0.525.9,2,0.523.9,0,-1.023.9,1,-1.0尝试解决方案def mB_calc (var1,var2,var3): df[var3]= np.where(df[var1] == 0, df[var2].shift(1) - df[var2] / -df[var1].shift(1) , "") return dfdf = mB_calc('days from new low','RSI on new low','mB') 首先,它给了我这个“TypeError:不能将序列乘以'float'类型的非整数”,其次我不知道如何将“ffill”合并到公式中。知道我怎么能做到吗?干杯!
1 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
试试这个:
df["mB_temp"] = (df["RSI on new low"].shift() - df["RSI on new low"]) / -df["days from new low"].shift()
df["mB"] = df["mB"].shift()
df["mB"].loc[df["days from new low"] == 0]=df["mB_temp"].loc[df["days from new low"] == 0]
df.drop(["mB_temp"], axis=1)
并与np.where:
df["mB"] = np.where(df["days from new low"]==0, df["RSI on new low"].shift() - df["RSI on new low"]) / -df["days from new low"].shift(), df["mB"].shift())
添加回答
举报
0/150
提交
取消