Reverting from Multi-Index to Single Index DataFrame in Pandas
Last Updated :
03 Dec, 2024
Let's learn how to revert a multi-index to a single index DataFrame in Pandas.
Setting Up a Multi-Index DataFrame in Pandas
To begin, let's import Pandas and load the data (data.csv):
Python
# Importing Pandas library
import pandas as pd
# Load the dataset into a DataFrame
df = pd.read_csv('data.csv')
# Displaying the first few rows of the dataset
df.head()
Output:
At this point, the DataFrame doesn't have any specific index, but we can create a multi-index using the set_index() method. We'll use the 'region', 'state', and 'individuals' columns as index levels:
Python
# Setting multi-index with 'region', 'state', and 'individuals'
df_mi = df.set_index(['region', 'state', 'individuals'])
# Display the DataFrame with multi-index
display(df_mi.head())
Output:
Multi-Index Pandas DataFrame Now, the DataFrame has a hierarchical index, commonly referred to as a multi-index.
Reverting from Multi-Index to Single Index using reset_index()
Pandas offers several ways to reset a multi-index, depending on your needs. The reset_index() method can be used in different ways to drop or retain certain index levels.
Method 1: Reverting Using Index Levels
You can specify the index levels you want to remove using the level parameter. In this case, we will remove 'region' (level 0) and 'individuals' (level 2), keeping 'state' (level 1) as the single index.
Python
# Reverting multi-index by removing level 0 and level 2
df_si_level = df_mi.reset_index(level=[0, 2])
# Display the DataFrame with a single index
display(df_si_level.head())
Output:
DataFrame with Single Index Method 2: Reverting Using Index Names
Alternatively, you can specify the index names directly in a list to reset them. Here, we will reset the 'region' and 'state' indexes, leaving 'individuals' as the index:
Python
# Reverting multi-index by specifying index names
df_si_name = df_mi.reset_index(['region', 'state'])
# Display the DataFrame with a single index
display(df_si_name.head())
Output:
Method 3: Removing All Indexes
If you want to completely remove the index from your DataFrame (i.e., convert it to an index-free DataFrame), you can pass all the index names to the reset_index() function:
Python
# Removing all index levels to make the DataFrame index-free
df_no_index = df_mi.reset_index(['region', 'state', 'individuals'])
# Display the index-free DataFrame
display(df_no_index.head())
Output:
Convert MultiIndex to Single Index using droplevel() Method
To drop one of the index levels, use the droplevel() method. In this example, we remove the 'number' level while keeping the 'letter' level.
Python
import pandas as pd
arrays = [['A', 'A', 'B', 'B'], ['one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=('letter', 'number'))
df = pd.DataFrame({'value': [10, 20, 30, 40]}, index=index)
print("Sample MultiIndex DataFrame:")
print(df)
# Dropping the second level ('number') and keeping 'letter' as the index
df_dropped = df.droplevel('number')
print("\nDataFrame after dropping the second level:")
print(df_dropped)
Output:
Similar Reads
Reset Index in Pandas Dataframe Letâs discuss how to reset the index in Pandas DataFrame. Often We start with a huge data frame in Pandas and after manipulating/filtering the data frame, we end up with a much smaller data frame. When we look at the smaller data frame, it might still carry the row index of the original data frame.
6 min read
Concatenate multiIndex into single index in Pandas Series In this article, we will see how to concatenate multi-index to a single index in Pandas Series. Multi-index refers to having more than one index with the same name. Create a sample series: Python3 # importing pandas module import pandas as pd import numpy as np # Creating series data for address det
3 min read
Pandas - How to reset index in a given DataFrame Let us see how to reset the index of a DataFrame after dropping some of the rows from the DataFrame.Approach :Â Import the Pandas module.Create a DataFrame.Drop some rows from the DataFrame using the drop() method.Reset the index of the DataFrame using the reset_index() method.Display the DataFrame
1 min read
How to drop rows in Pandas DataFrame by index labels? Dropping rows in a Pandas DataFrame by index labels is a common operation when you need to remove specific rows based on their index positions. For example, we are working with a DataFrame that contains details of students, including their names, ages, and universities. Initially, the DataFrame has
5 min read
How to Reverse Row in Pandas DataFrame? In this article, we will learn how to reverse a row in a pandas data frame using Python. With the help of Pandas, we can perform a reverse operation by using loc(), iloc(), reindex(), slicing, and indexing on a row of a data set. Creating Dataframe Letâs create a simple data frame with a dictionar
3 min read
Reshape Wide DataFrame to Tidy with identifiers using Pandas Melt Sometimes we need to reshape the Pandas data frame to perform analysis in a better way. Reshaping plays a crucial role in data analysis. Pandas provide functions like melt and unmelt for reshaping. In this article, we will see what is Pandas Melt and how to use it to reshape wide to Tidy with identi
3 min read
Pandas DataFrame.reset_index() In Pandas, reset_index() method is used to reset the index of a DataFrame. By default, it creates a new integer-based index starting from 0, making the DataFrame easier to work with in various scenarios, especially after performing operations like filtering, grouping or multi-level indexing. Example
3 min read
How to Merge Two Pandas DataFrames on Index Merging two pandas DataFrames on their index is necessary when working with datasets that share the same row identifiers but have different columns. The core idea is to align the rows of both DataFrames based on their indices, combining the respective columns into one unified DataFrame. To merge two
3 min read
Python | Pandas DataFrame.set_index() Pandas DataFrame.set_index() method sets one or more columns as the index of a DataFrame. It can accept single or multiple column names and is useful for modifying or adding new indices to your DataFrame. By doing so, you can enhance data retrieval, indexing, and merging tasks.Syntax: DataFrame.set_
3 min read