# Creating new variables inside a for loop

**URL:** <https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374>\
**Category:** Python Help\
**Created:** [June 10, 2024, 12:35pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374 "2024-06-10T12:35:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![connor\_g9](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@connor\_g9](https://discuss.python.org/u/connor_g9)\
**Post date:** [June 10, 2024, 12:35pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/1 "2024-06-10T12:35:43Z")

</div>

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:

 ![Screenshot 2024-06-10 at 8.30.01 AM](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/d/f/df1b6a7ddae522fd4371145e7a34871283b88103.png)

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:

```python
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!

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [June 10, 2024, 1:06pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/2 "2024-06-10T13:06:51Z")

</div>

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:

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

```

This does not get you radians on the left term.

---

<div class="post-metadata">

**Author:** ![connor\_g9](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@connor\_g9](https://discuss.python.org/u/connor_g9)\
**Post date:** [June 10, 2024, 1:18pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/3 "2024-06-10T13:18:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [June 10, 2024, 1:23pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/4 "2024-06-10T13:23:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![connor\_g9](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@connor\_g9](https://discuss.python.org/u/connor_g9)\
**Post date:** [June 10, 2024, 1:38pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/5 "2024-06-10T13:38:10Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [June 10, 2024, 1:46pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/6 "2024-06-10T13:46:31Z")

</div>

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

```python
= `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.

---

<div class="post-metadata">

**Author:** ![connor\_g9](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@connor\_g9](https://discuss.python.org/u/connor_g9)\
**Post date:** [June 10, 2024, 1:49pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/7 "2024-06-10T13:49:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [June 10, 2024, 1:59pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/8 "2024-06-10T13:59:01Z")

</div>

Assuming you have created a list, as in:

```python
x_angle = []

```

Then, to add values incrementally, you do:

```python
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):

```python
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.

---

<div class="post-metadata">

**Author:** ![connor\_g9](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@connor\_g9](https://discuss.python.org/u/connor_g9)\
**Post date:** [June 10, 2024, 2:04pm UTC](https://discuss.python.org/t/creating-new-variables-inside-a-for-loop/55374/9 "2024-06-10T14:04:04Z")

</div>

Sounds good! Thank you for the help!
