简体   繁体   中英

python - how do I perform the below operation in python dataframe

below is my df

df = pd.DataFrame({
                   'Sr. No': [1, 2, 3, 4, 5, 6],
                    'val1' : [2,3,2,4,1,2],

})

I want output val2 as show in the below figures. row1 is same as row1 of val1 but row2 and below is calculated using a formula, as shown

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM