
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to reset index in Pandas dataframe?
In this program, we will replace or, in other words, reset the default index in the Pandas dataframe. We will first make a dataframe and see the default index and then replace this default index with our custom index.
Algorithm
Step 1: Define your dataframe. Step 2: Define your own index. Step 3: Replace the default index with your index using the reset function in Pandas library.
Example Code
import pandas as pd dataframe = {'Name':["Allen", "Jack", "Mark", "Vishal"],'Marks':[85,92,99,87]} df = pd.DataFrame(dataframe) print("Before using reset_index:\n", df) own_index = ['a', 'j', 'm', 'v'] df = pd.DataFrame(dataframe, own_index) df.reset_index(inplace = True) print("After using reset_index:\n", df)
Output
Before using reset_index(): Name Marks 0 Allen 85 1 Jack 92 2 Mark 99 3 Vishal 87 After using reset_index(): index Name Marks 0 0 Allen 85 1 1 Jack 92 2 2 Mark 99 3 3 Vishal 87
- Related Articles
- How to reset hierarchical index in Pandas?
- Python - How to reset index after Groupby pandas?
- How to display Pandas Dataframe in Python without Index?
- How to plot a Pandas multi-index dataFrame with all xticks (Matplotlib)?
- Create a DataFrame with customized index parameters in Pandas
- Python Pandas - Display the index of dataframe in the form of multi-index
- How to plot a time as an index value in a Pandas dataframe in Matplotlib?
- Select DataFrame rows between two index values in Python Pandas
- Python Pandas - Create a DataFrame from original index but enforce a new index
- Python Pandas - Create a DataFrame from DateTimeIndex ignoring the index
- Python Pandas – How to use Pandas DataFrame Property: shape
- Python Pandas – How to use Pandas DataFrame tail( ) function
- Python – Drop a level from a multi-level column index in Pandas dataframe
- Python – Drop multiple levels from a multi-level column index in Pandas dataframe
- Python - Rename column names by index in a Pandas DataFrame without using rename()

Advertisements