Home » Pandas: How to Add New Column with Row Numbers

Pandas: How to Add New Column with Row Numbers

by Tutor Aspire

There are two ways to add a new column that contains row numbers in a pandas DataFrame:

Method 1: Use assign()

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

Method 2: Use reset_index()

df['row_number'] = df.reset_index().index

Both methods produce the same result.

The following examples show how to use each method  in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [5, 17, 7, 19, 12, 13, 9, 24],
                   'assists': [4, 7, 7, 6, 8, 7, 10, 11]})

#view DataFrame
print(df)

  team  points  assists
0    A       5        4
1    B      17        7
2    C       7        7
3    D      19        6
4    E      12        8
5    F      13        7
6    G       9       10
7    H      24       11

Example 1: Use assign() to Add Row Number Column

The following code shows how to use the assign() function to add a new column called row_number that displays the row number of each row in the DataFrame:

#add column that contains row numbers
df = df.assign(row_number=range(len(df)))

#view updated DataFrame
print(df)

  team  points  assists  row_number
0    A       5        4           0
1    B      17        7           1
2    C       7        7           2
3    D      19        6           3
4    E      12        8           4
5    F      13        7           5
6    G       9       10           6
7    H      24       11           7

Notice that the values in the row_number column range from 0 to 7.

Example 2: Use reset_index() to Add Row Number Column

The following code shows how to use the reset_index() function to add a new column called row_number that displays the row number of each row in the DataFrame:

#add column that contains row numbers
df['row_number'] = df.reset_index().index

#view updated DataFrame
print(df)

  team  points  assists  row_number
0    A       5        4           0
1    B      17        7           1
2    C       7        7           2
3    D      19        6           3
4    E      12        8           4
5    F      13        7           5
6    G       9       10           6
7    H      24       11           7

Notice that the values in the row_number column range from 0 to 7.

This matches the results from the previous example.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Pandas: How to Find the Difference Between Two Columns
Pandas: How to Find the Difference Between Two Rows
Pandas: How to Subtract Two Columns

You may also like