Skip to content
Advertisement

Simplest way to add repeating counter column to pandas dataframe

I have a datafame:

JavaScript

What is the shortes / simples way to add a repeating counter column like this?:

JavaScript

My feeling tells me, that there must be a one-line solution (maybe a bit longer). But all I can think of would be much longer and complex.

How would you approach this?

Advertisement

Answer

try:

JavaScript

OR

If you have a custom index then you can use:

JavaScript

output of df:

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