

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to reset hierarchical index in Pandas?
- Python - How to reset index after Groupby pandas?
- How to display Pandas Dataframe in Python without Index?
- Create a DataFrame with customized index parameters in Pandas
- How to plot a Pandas multi-index dataFrame with all xticks (Matplotlib)?
- Python Pandas - Display the index of dataframe in the form of multi-index
- 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
- How to plot a time as an index value in a Pandas dataframe in Matplotlib?
- Python Pandas – How to use Pandas DataFrame Property: shape
- Python Pandas – How to use Pandas DataFrame tail( ) function
- Python Pandas - Create a DataFrame with both the original index and name
- Python - Rename column names by index in a Pandas DataFrame without using rename()
- Python - How to Group Pandas DataFrame by Month?
Advertisements