add a column in python with rows number

Question:

I have a dataset like these

state sex birth player
QLD m 1993 Dave
QLD m 1992 Rob

Now I would like to create an additional row, which is the id. ID is equal to the row number but + 1

df = df.assign(ID=range(len(df)))

But unfortunately the first ID is zero, how can I fix that the first ID begins with 1 and so on

I want these output

state sex birth player ID
QLD m 1993 Dave 1
QLD m 1992 Rob 2

but I got these

state sex birth player ID
QLD m 1993 Dave 0
QLD m 1992 Rob 1

How can I add an additional column to python, which starts with one and gives for every row a unique number so for the second row 2, third 3 and so on.

Asked By: J_Martin

||

Answers:

You can try this:

import pandas as pd

df['ID'] = pd.Series(range(len(df))) + 1
Answered By: Csaba
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.