Creating new variables inside a for loop

Hello! I am a relatively new python user with little experience in for loops, and one is definitely necessary for my project. I have a pandas dataframe that looks like this:

And I need to “integrate” the accel columns their respective velocity (and eventually position) and the angular into degrees. However, since the first row needs to be all 0s, there has to be two conditions inside the for loop: first row, and then everything else. The code I have so far looks like this:

for i in df:
    if i == 0:
        x_angle[i] = df["X_Angular"[i]] * (df["Time"[i]]-df["Time"[i-1]])
        y_angle[i] = df["Y_Angular"[i]] * (df["Time"[i]]-df["Time"[i-1]])
        z_angle[i] = df["Z_Angular"[i]] * (df["Time"[i]]-df["Time"[i-1]])
        
        x_velo[i] = df["X_Accel"[i]] * (df["Time"[i]]-df["Time"[i-1]])
        y_velo[i] = df["Y_Accel"[i]] * (df["Time"[i]]-df["Time"[i-1]])
        z_velo[i] = df["Z_Accel"[i]] * (df["Time"[i]]-df["Time"[i-1]])

    if i >= 1:
        x_angle[i] = df["X_Accel"[i-1]] + (df["X_Angular"[i]] * (df["Time"[i]]-df["Time"[i-1]]))
        y_angle[i] = df["Y_Accel"[i-1]] + (df["Y_Angular"[i]] * (df["Time"[i]]-df["Time"[i-1]]))
        z_angle[i] = df["Z_Accel"[i-1]] + (df["Z_Angular"[i]] * (df["Time"[i]]-df["Time"[i-1]]))

        x_vector[i] = df["X_Accel"[i]] + (df["X_Accel"[i]] * math.acos(df["Z_Angle"[i]])) + (df["X_Accel"[i]] * math.acos(df["Y_Angle"[i]]))
        y_vector[i] = df["Y_Accel"[i]] + (df["Y_Accel"][i] * math.acos(df["X_Angle"][i])) + (df["Y_Accel"][i] * math.acos(df["Z_Angle"][i]))
        z_vector[i] = df["Z_Accel"[i]] + (df["Z_Accel"][i] * math.acos(df["X_Angle"][i])) + (df["Z_Accel"][i] * math.acos(df["Y_Angle"][i]))
        total_a = sqrt(x_vector^2 + y_vector^2 + z_vector^2) - 1

I don’t exactly need a specific answer to my case, just how to create new variables each time the for loop iterates through each row. Thanks!

Hi,

for the case when if i >= 1:, what are the units for:

  1. df["X_Accel"[i-1]]r/s^2 … here I am assuming radians per second squared
  2. df["X_Angular"[i]]r
  3. (df["Time"[i]]-df["Time"[i-1]])s

So, if we substitute only the units, we should get radians r in our final result. In its current form, the expressions is:

angle =  `r/s^2` + `r` * (`s` - `s`) = `r/s^2` + `rs`

This does not get you radians on the left term.

Thanks for correcting the math! The new variables should simply be assigned into a new list or dataframe, which is what I was hoping the x_angle[i] would do, but it appears that that is not the correct way to do it. Basically, I am looking for a way to keep the original values and more or less create a new dataframe (or list, but that is easily convertible) that corresponds row-by-row to the original. The new dataframe would look nearly identical to the first, only that the values are changed (ideally correctly) and the columns would be x_velocity and x_angle instead of x_accel and x_angular.

Please take a look at my updated response. Even if we change the + to *, it still won’t give you radians as the final result. Please check that you are setting up the expression correctly.

Can you state the units of each variable (term) in the expression?

Sure! The accel columns are in terms of m/s^2, and the angular columns are in terms of deg/sec. The time column is in seconds (I have updated the values because it is supposed to be at 1.6… seconds, not 16).

Ok, thank you. If that is true, and we substitute only the units, we have on the right side of the expression:

= `m/s^2 + deg/s * s`  # The `s` cancel out
= `m/s^2 + deg`

You see, this does not give you degrees on the left side of the expression. You first have to make sure that you are setting up your expressions correctly and that the correct units are being used.

Thanks for pointing that out, I will get to fixing that asap. As far as the declaration goes, is x_angle[i] correct for creating a new variable each time the loop iterates?

Assuming you have created a list, as in:

x_angle = []

Then, to add values incrementally, you do:

for i in range(10):
    x_angle.append(i)  # Here, I am just filling it in with index values

print('x_angle = :', x_angle)

In your case, you can include the expression inside the parenthesis, as in (here is a general example):

x_angle = []

c = 2
h = 4.5

for i in range(10):
    x_angle.append(i**c - h)  # Here, I am just filling it in with misc expression

print(x_angle)

if not, then just use a temp variable as an intermediary, then include it inside the parenthesis to append the value into the list. Experiment so that you become familiar with Python.

Sounds good! Thank you for the help!