Trying to create a column with values from 0 and 2 based on a step size (python)

I am new to python loops. I am using pandas and have created a blank data frame. I am trying to append values from 0 through 2 based on a given step size in a list. For each step size, I need to append a new column in the data frame that goes from 0 to 2.

Here is what the output looks like (with the first three step sizes as examples):

enter image description here

Here is what I have tried:

import numpy as np

seq = [0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625]
df = pd.DataFrame()

for delta_x in seq:
    df[delta_x] = 0
    while df[delta_x] < 0:
        df[delta_x] + delta_x

However, the following error is thrown:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What can I do to fix this?

>Solution :

I believe you are looking for the numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None) function. The result of this method can be used in the Series constructor.

import pandas as pd
import numpy as np

delta = 0.125
s = pd.Series(np.arange(0, 2 + delta, step=delta))

Leave a Reply