'How to create a number iteration in function of row

I got a problem in python, but I rarely use python to be honest, I tried to search on doc about library... I found the pandas library but I didn't succeed about what I wanted.

My aim is just to create a number which correspond to the row of my dataframe.

I have this :


Column A    column B    column C  column D   .. . . . .  column G
2323         EEE         22        RR
32323        RT          23        RR
55           ERT         23       RT
4545         EZHH        24       TT
545          RER         25       YY
455          ERT         26       YY

And my goal is to add a new column like this :

Column A    column B    column C  column D   .. . . . .  column G   new column
2323         EEE         22        RR                                 1
32323        RT          23        RR                                 2
55           ERT         23       RT                                  3
4545         EZHH        24       TT                                  4
545          RER         25       YY                                  5
455          ERT         26       YY                                  6

I tried to do use pandas with pd.dataframe and assign, I did a loop for but it always put a syntax error.

Thanks for reading me



Solution 1:[1]

If the index is not natural number, you can assign a list to column directly,

df['new col'] = range(len(df))
# or depend on where you want to start from
df['new col'] = range(1, len(df)+1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynjxsjmh