user13412850
user13412850

Reputation: 519

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

enter image description here

enter image description here

enter image description here

Upvotes: 0

Views: 54

Answers (1)

mlang
mlang

Reputation: 762

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)

Upvotes: 1

Related Questions