CentauriAurelius
CentauriAurelius

Reputation: 504

Python create column of repeated values that matches length of dataframe

I need to create a column that repeats an initial value 32 times, then increases it by 1 and repeats 32 times, etc.. until it is the length of my original dataframe.

I tried doing the following (and other minor variants):

timestamps = [1506447570]
i = 0
k = 0
for i in range(len(df)):
    timestamps.append(timestamps[i])
    k+=1
    i+= 1
    if k >31:
        timestamps.append(timestamps[i]+1)
        k =0

This is what it should look like for the first 2 repetitions:

timestamps = [
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447570,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571,
1506447571]

So it should continue repeating this pattern until it is the length of the dataframe. The values are unix timestamps and the data is collected at 32HZ so I'm just trying to make a simple timestamps column.

Upvotes: 1

Views: 1916

Answers (2)

Eric Ed Lohmar
Eric Ed Lohmar

Reputation: 1922

You can use len on df.index to get the length of your dataframe. From there, you can iterate over a range on that length and use division and floor to change the numbers.

from math import floor
val = 1506447570

df['stamps'] = [val + floor(i / 32) for i in range(len(df.index))]

Upvotes: 3

Kyle Swanson
Kyle Swanson

Reputation: 1428

Give this ago :)

acc = 2
seedStamp = 1506447570
timeStamps = []

for i in range(acc):
  for k in range(33):
    timeStamps.append(seedStamp)
    if k >31:
      seedStamp += 1
      timeStamps.append(seedStamp)

print('--')

for i in timeStamps:
  print(i)

Upvotes: 0

Related Questions