Skip to content
Advertisement

How to create a new column conditionally?

I want to add a new column called id to my pandas DataFrame. If the value of id is just a fixed number, then I can create a new column as follows:

JavaScript

However, in my case I need to create batches of 3 rows, as follows:

JavaScript

How can I do it?

Advertisement

Answer

You can create array by np.arange with integer division by 3, so starting by 0, if need custom first number add id_value:

JavaScript

If there is default index values also working:

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