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:
1 2 3 4 | ### Create dataframe df = pd.DataFrame([[ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ]]) df |
Resultant Dataframe:
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
1 2 3 4 | df2 = pd.concat([df, df]) df2 df2.reset_index() |
so, the resultant dataframe
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.
1 2 3 | df3 = pd.concat([df, df]) df3.reset_index(drop = True , inplace = True ) df3 |
so, the resultant dataframe