I have a dataframe like:
principalDf =
| x || y || z || values |
| x0 || y0 || z0 || areal |
| x1 || y1 || z1 || aimag |
| x2 || y2 || z2 || breal |
| x3 || y3 || z3 || bimag |
I want to produce:
frobprincipalDf =
| x || y || z || values |
|sqrt(x0^2+x1^2)||sqrt(y0^2+y1^2)||sqrt(z0^2+z1^2)|| a |
|sqrt(x2^2+x3^2)||sqrt(y2^2+y3^2)||sqrt(z2^2+z3^2)|| b |
I'm generating the required x, y and z data by:
frobprincipalDf = pd.DataFrame(columns=principalDf.columns)
for col in principalDf.columns[:-1]:
for index in range(0,len(principalDf),2):
frobnorm = np.sqrt(principalDf[col].iloc[index]**2+principalDf[col].iloc[index+1]**2)
print(frobnorm)
How can I add frobnorm to a specific col in the newly created frobprincipalDf?
Is there a better way to do this than nested for loops?
It should be generalisable for any value name, such that if another value begins with a, it will not 'group' them together.
It looks as though the below is a complete solution to the initial problem of even-index pairs of rows addition. This is slightly modified answer of quasi-human's answer. The secondary problem of adding a single value to a specific df column is still there though.
frobprincipalDf = np.sqrt(principalDf[['x', 'y', 'z']].shift() ** 2 + principalDf[['x', 'y', 'z']] ** 2)
frobprincipalDf = pd.concat([frobprincipalDf,principalDf['values'].str[:2]], axis=1)
frobprincipalDf = frobprincipalDf[frobprincipalDf.index % 2 == 1].reset_index(drop=True)