I have a dataframe:
A B C v1 v2 v3
q 2 3 4 9 1
8 f 2 7 4 7
I want to calc a new columns, that will have the RMS (sqrt(sum(x^2)) of all the v columns.
So the new df will be:
A B C v1 v2 v3 v_rms
q 2 3 4 9 1 9.9
8 f 2 7 2 4 8.3
since sqrt(4^2 9^2 1^2) = 9.9, sqrt(7^2 2^2 4^2) = 8.3
What is the best way to do so?
CodePudding user response:
Use DataFrame.filter for v columns, then DataFrame.pow with sum and for sqrt is used pow with 1/2:
df['v_rms'] = df.filter(like='v').pow(2).sum(axis=1).pow(1/2)
print (df)
A B C v1 v2 v3 v_rms
0 q 2 3 4 9 1 9.899495
1 8 f 2 7 2 4 8.306624
