When we look at a smaller data frame, it may still contain the row index of the original data frame. If the original index is numbers , we now have indices that are not contiguous. Well, pandas have reset_index ()
. So, to reset the index to the default integer index starting at 0, we can simply use .reset_index ()
.
So let’s see how we can reset the DataFrame’s index.
Check out the original DataFrame first.
# Import pandas package import pandas as pd # Define a dictionary containing employee data data = { ’ Name’ : [ ’Jai’ , ’ Princi’ , ’Gaurav’ , ’Anuj’ , ’ Geeku’ ], ’Age’ : [ 27 , 24 , 22 , 32 , 15 ], ’Address’ : [ ’Delhi’ , ’ Kanpur’ , ’Allahabad’ , ’ Kannauj’ , ’Noida’ ], ’Qualification’ : [ ’ Msc’ , ’MA’ , ’MCA’ , ’ Phd’ , ’10th’ ]} # Convert dictionary to DataFrame df = pd.DataFrame (data) df |
Exit:
Example # 1: Create your own index without removing the default index.
# Import pandas package import pandas as pd # Define a dictionary containing employee data data = { ’Name’ : [ ’Jai’ , ’ Princi’ , ’Gaurav’ , ’ Anuj’ , ’Geeku’ ], ’Age’ : [ 27 , 24 , 22 , 32 , 15 ] , ’Address’ : [ ’Delhi’ , ’ Kanpur’ , ’Allahabad’ , ’ Kannauj’ , ’Noida’ ], ’Qualification’ : [ ’ Msc’ , ’MA’ , ’MCA’ , ’ Ph d’ , ’10th’ ]} index = { ’a’ , ’b’ , ’ c’ , ’d’ , ’ e’ } # Convert dictionary to DataFrame df = pd.DataFrame (data, index) # Make your own index as an index # In this case, the default index exists df.reset_index (inplace = True ) df |
Output:
Example # 2: Create your own index and drop the default index.
# Import pandas package import pandas as pd # Define a dictionary containing employee data data = { ’Name’ : [ ’ Jai’ , ’Princi’ , ’Gaurav’ , ’Anuj’ , ’ Geeku’ ], ’Age’ : [ 27 , 24 , 22 , 32 , 15 ], ’Address’ : [ ’ Delhi’ , ’ Kanpur’ , ’All ahabad’ , ’Kannauj’ , ’Noida’ ], ’Qualification’ : [ ’ Msc’ , ’MA’ , ’ MCA’ , ’Phd’ , ’ 10th’ ]} # Create own index index = { ’a’ , ’ b’ , ’ c’ , ’d’ , ’ e’ } # Convert dictionary to DataFrame # Create your own index and drop the default index df = pd.DataFrame (data, index) df |
Exit:
Example 3: Reset your own index and make the default index as the index.
# Import pandas package import pandas as pd # Define a dictionary containing employee data data = { ’Name’ : [ ’ Jai’ , ’ Princi’ , ’Gaurav’ , ’Anuj’ , ’ Geeku’ ], ’Age ’ : [ 27 , 24 , 22 , 32 , 15 ], ’Address’ : [ ’ Delhi’ , ’Kanpur’ , ’ Allahabad’ , ’Kannauj’ , ’ Noida’ ], ’Qualification’ : [ ’Msc’ , ’ MA’ , ’MCA’ , ’ Phd’ , ’10th’ ]} # Create own index index = { ’a’ , ’ b’ , ’ c’ , ’d’ , ’e’ } # Convert dictionary to DataFrame df = pd.DataFrame (data, index) # delete custom index with default index df.reset_index (inplace = True , drop = True ) df |
Output:
Example # 4: Make data column as index deleting the default index.
# Import pandas package import pandas as pd # Define a dictionary containing employee data data = { ’ Name’ : [ ’Jai’ , ’ Princi’ , ’Gaurav’ , ’Anuj’ , ’ Geeku’ ], ’ Age’ : [ 27 , 24 , 22 , 32 , 15 ], ’ Address’ : [ ’Delhi’ , ’Kanpur’ , ’Allahabad’ , ’ Kannauj’ , ’Noida’ ], ’Qualification’ : [ ’ Msc’ , ’MA’ , ’ MCA’ , ’Phd’ , ’10th’ ]} # Create your own index index = { ’a’ , ’b’ , ’ c’ , ’d’ , ’ e’ } # Convert dictionary to DataFrame df = pd.DataFrame (data, index ) # set the index of any column of our DF and # remove default index df.set_index ([ ’ Age’ ], inplace = True ) df |
Output:
Example 5. Creating a data column as an index without dropping the y index silence.
# Import pandas package import pandas as pd # Define a dictionary containing employee data data = { ’Name’ : [ ’ Jai’ , ’Princi’ , ’ Gaurav’ , ’Anuj’ , ’ Geeku’ ], ’Age’ : [ 27 , 24 , 22 , 32 , 15 ], ’Address’ : [ ’ Delhi’ , ’Kanpur’ , ’ Allahabad’ , ’Kannauj’ , ’ Noida’ ], ’Qualification’ : [ ’Msc’ , ’ MA’ , ’MCA’ , ’Phd’ , ’ 10th’ ]} # Create your own index index = { ’a’ , ’ b’ , ’c’ , ’ d ’ , ’ e’ } # Convert dictionary to DataFrame df = pd.DataFrame (data, i ndex) # set any column as index # Here we set the age column as index df.set_index ([ ’Age’ ], inplace = True ) # reset index without deleting default index df.reset_index (level = [ ’Age’ ], inplace = True ) df |
Exit d: