Python Pandas - Start row index from 1 instead of zero without creating additional column

In this article, we are going to change the format of indexing, we will start row index from 1 instead of zero. By Pranit Sharma Last updated : September 26, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Index in Pandas DataFrame

Index in pandas is just the number of rows defined in a Series or DataFrame. The index always starts from 0 to n-1 where n is the number of rows. The index is used in indexing which is a method of traversing or selecting particular rows and columns or selecting all the rows and columns.

Start row index from 1 instead of zero without creating additional column

To start row index from 1 instead of zero without creating additional column, we will achieve this task without creating an additional column. For this purpose, we will use np.arrange() method which will help us to rearrange the index of this DataFrame, we will pass the range as (1,len(df)+1) inside this method so that the row will start from index 1 instead of index 0.

Let us understand with the help of an example,

Python program to start row index from 1 instead of zero without creating additional column

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'Name':["Pranit","Shobhit","Riyansh","Abhishek","Parav","Jeetendra"],
    'Grades':['A','C','A','B','D','E']
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")

# Rearranging index
df.index = np.arange(1, len(df) + 1)

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Start row index from 1 instead of zero

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.