Table of Contents
Adding a new column to a Pandas DataFrame with row numbers can be achieved by using the “reset_index” function in Pandas. This function creates a new column with the row numbers and resets the index of the DataFrame. By specifying the parameter “drop=True”, the old index can be dropped to avoid redundancy. This new column can then be renamed to suit the user’s preference. This method is useful for organizing and tracking data in a DataFrame, especially when dealing with large datasets.
Pandas: Add New Column with Row Numbers
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 DataFrameprint(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 7Notice 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 7Notice that the values in the row_number column range from 0 to 7.
This matches the results from the previous example.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I add a new column to a Pandas DataFrame with row numbers?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-new-column-to-a-pandas-dataframe-with-row-numbers/
stats writer. "How can I add a new column to a Pandas DataFrame with row numbers?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-new-column-to-a-pandas-dataframe-with-row-numbers/.
stats writer. "How can I add a new column to a Pandas DataFrame with row numbers?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-new-column-to-a-pandas-dataframe-with-row-numbers/.
stats writer (2024) 'How can I add a new column to a Pandas DataFrame with row numbers?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-new-column-to-a-pandas-dataframe-with-row-numbers/.
[1] stats writer, "How can I add a new column to a Pandas DataFrame with row numbers?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add a new column to a Pandas DataFrame with row numbers?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
