Reset index in pandas using “reset_index” method of pandas dataframe.
When we perform slicing or filtering operations on Pandas Dataframe, the resulting dataframe retains original indices which might cause issues in further index based operations. In the below example we first create a pandas dataframe then we filter on the basis of color. Finally we reset the index of filtered dataframe.
# Imports
import pandas as pd
# Let's create a dataframe
data = {'Color': ['Red', 'Red', 'Green', 'Blue', 'Red', 'Green'],
'Shape': ['Circle', 'Square', 'Square', 'Triangle', 'Circle', 'Triangle'],
'Value': [1, 1, 2, 1, 3, 3]}
df = pd.DataFrame(data)
df
Color | Shape | Value | |
---|---|---|---|
0 | Red | Circle | 1 |
1 | Red | Square | 1 |
2 | Green | Square | 2 |
3 | Blue | Triangle | 1 |
4 | Red | Circle | 3 |
5 | Green | Triangle | 3 |
# Let's filter green color entries
greens = df[df.Color == 'Green']
greens
Color | Shape | Value | |
---|---|---|---|
2 | Green | Square | 2 |
5 | Green | Triangle | 3 |
greens.index
Int64Index([2, 5], dtype='int64')
# To reset index
greens.reset_index(drop=True, inplace=True)
# Drop will drop the original index
greens
Color | Shape | Value | |
---|---|---|---|
0 | Green | Square | 2 |
1 | Green | Triangle | 3 |
Related Resources:
- Pandas groupby tutorial | Understand Group by Pandas Groupby Group by is an important technique in Data Analysis and Pandas groupby method helps us achieve it. In...
- Not Operation in Pandas Conditions | Pandas tutorial Not Operation in Pandas Conditions Apply not operation in pandas conditions using (~ | tilde) operator. In this Pandas tutorial...
- One Hot Encoding | What is one hot encoding? One Hot Encoding | Dummies One Hot encoding means splitting categorical variable into multiple binary variables. “One hot” means at...
- Pandas Series Index | Pandas tutorial Create Pandas Series with Custom index Create Pandas Series with custom index using “Series” method of Pandas library and index...
- Pandas series from Dictionary | Pandas Tutorial Pandas Series from Dictionary Create pandas series from dictionary using “Series” method of Pandas library. In the below example we...
- Create Pandas series | Pandas tutorial Create Pandas Series Create Pandas Series with “Series” method of Pandas library. In this Pandas tutorial we are creating a...
- Save pandas dataframe to csv or excel file ( 2 Ways to Save pandas dataframe) Save Pandas dataframe to csv and excel file Use “to_csv” and “to_excel” methods to save pandas dataframe to csv and...
- Gini Index vs Entropy Information gain | Decision Tree | No 1 Guide Gini index vs Entropy Gini index and entropy is the criterion for calculating information gain. Decision tree algorithms use information...
- 8 Ways to Drop Columns in Pandas | A Detailed Guide 8 Ways to Drop Columns in Pandas Often there is a need to modify a pandas dataframe to remove unnecessary...
- Pandas Dataframe Slicing | Select specific entries Dataframe Slicing Pandas Datafrane slicing is one of the most important operations. Here’s how to do slicing in a pandas...