Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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)defcalc_val2(val1_vals, sr_no_vals):
    val2_vals = [val1_vals[0]]
    for i inrange(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)

Post a Comment for "Python - How Do I Perform The Below Operation In Python Dataframe"