Hello, good evening.
I have a set of vectors whose components (px, py, and pz) are stored in a Pandas DataFrame. I have written a function whose purpose is to change the signal of the vector components if the following condition is observed:
If the value of pz is negative, then all components (including pz) of the vector must be multiplied by -1.
Below is the code I've tried so far:
Test DataFrame:
df = pd.DataFrame(np.random.randn(5, 3),
index=['vec1', 'vec2', 'vec3', 'vec4', 'vec5'],
columns=['px', 'py', 'pz'])
The function:
def change_sign(df):
for value in df['pz'].values:
if value >= 0:
df['px_1'] = df['px']
df['py_1'] = df['py']
df['pz_1'] = df['pz']
else:
df['px_1'] = -df['px']
df['py_1'] = -df['py']
df['pz_1'] = -df['pz']
return df
Role application:
change_sign(df)
The problem is that when I apply the function, the components of all vectors, even when pz is positive, are multiplied by -1. This should only happen when pz is less than 0.
I'm stuck because I'm not sure what I'm forgetting to do or what I'm doing wrong.
I'm running a virtual environment on Python 3.7.1 macros (Mini-deck 4.5.11), with pandas 0.23.4 and numpy 0.15.4.
Any help will be greatly appreciated.
Thank you.