Skip to content
Advertisement

Using a column of values to create a counter for a variable sequential number column

I currently have a pandas dataframe with some columns. I’m looking to build a column, Sequential, that lists what iteration is recorded at that part of the cycle. I’m currently doing this using itertools.cycle, and a fixed number of iterations block_cycles, like so:

JavaScript

With an output like this:

JavaScript

And this would be fine, if the number of iterations in a cycle was the same. However, upon investigation, I’ve found that not every cycle contains the same amount of iterations. I have another column, CycleNumber, that contains what cycle number the iteration belongs to. It looks like this:

JavaScript

So, for example, one cycle might contain 330 iterations, and another could contain 333, 331, and so forth – it’s not guaranteed to be the same. The values in cycle number increase incrementally.

I’ve built a dictionary of the amount of iterations each cycle contains, cycle_freq, which looks like this:

JavaScript

How could I go about using this dictionary to replace the constant variable block_cycles, creating a big column list of sequential numbers based on exactly how many iterations were in that cycle? So far, this is my logic to try to get it to use the values contained in the dictionary cycle_freq, but to no avail:

JavaScript

My desired output would look like this:

JavaScript

Any help would be greatly appreciated!

Advertisement

Answer

I’ve used a workaround and gave up itertools:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement