So all rows are dependent on the previous as C4 depends on the calculation of C3 for instance. So what we can do is to operate on the numpy arrays directly.
sr_no_vals = df['Sr. No'].values
val1_vals = df['val1'].values
val2_vals = [val1_vals[0]]
for i in range(1, len(sr_no_vals)):
calculated_value = (((1 + val2_vals[i - 1]) ** sr_no_vals[i - 1]) * (1 + val1_vals[i])) ** (1 / sr_no_vals[i])
val2_vals.append(calculated_value)
df['val2'] = val2_vals
When operating with numpy arrays, we can also use a just-in-time compiler such as numba to speed up the operation by a huge factor for large data.
@numba.jit(nopython=True)
def calc_val2(val1_vals, sr_no_vals):
val2_vals = [val1_vals[0]]
for i in range(1, len(sr_no_vals)):
calculated_value = (((1 + val2_vals[i - 1]) ** sr_no_vals[i - 1]) * (1 + val1_vals[i])) ** (1 / sr_no_vals[i])
val2_vals.append(calculated_value)
return val2_vals
df['val2'] = calc_val2(val1_vals, sr_no_vals)
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.