Pandas Reset Index without adding new column

In Pandas when you reset the index new column named “index” will be added, Our Aim is to avoid this scenario, this can be achieved using reset_index() with drop=True, like reset_index(drop=True). So in this section we will focus on how to reset the index in pandas without adding new column.

Let’s see with an Example

 

Create dataframe:

 
### Create dataframe

df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
df

Resultant Dataframe:

Pandas-Reset-Index-without-adding-new-column-1

 

 

Reset index in pandas dataframe by adding new column:

Now we concatenate two dataframe using reset_index(). when we concatenate and do reset index then old index is stored as new column and then the index is resetted

 

df2 = pd.concat([df, df])
df2

df2.reset_index()

so, the resultant dataframe

Pandas-Reset-Index-without-adding-new-column-2

 

 

Pandas Reset Index without adding new column

The additional column can be avoided by dropping the index using reset_index() with drop=True, like reset_index(drop=True) as shown below.

 

df3 = pd.concat([df, df])
df3.reset_index(drop=True, inplace=True)
df3

so, the resultant dataframe

Pandas-Reset-Index-without-adding-new-column-3

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts