JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Flattening Hierarchical Indexes in Pandas Columns

Author:JIYIK Last Updated:2025/04/12 Views:

This article will discuss how to flatten a hierarchical index in a Pandas Dataframe column.

Groupby aggregation functions are often used to create hierarchical indexes. The used aggregation function will be visible in the hierarchical index of the resulting DataFrame.

We will use different functions to explain how to flatten a hierarchical index across columns.


rest_index()Flattening a Hierarchical Index in a Pandas Column Using

The function in Pandas reset_index()flattens the hierarchical index created by the groupby aggregation function.

grammar:

pandas.DataFrame.reset_index(level, drop, inplace)

in,

  • level: Remove only the indicated level from the index.
  • drop: Reset the index to the default integer index.
  • inplace: Do not copy, permanently modify the DataFrame object.

We use the Pandas groupby()function to group the bus sales data by quarter and reset_index()the pandas function to flatten the hierarchical index column of the grouped DataFrame.

First, we import the Python Pandas library and then create a simple DataFrame. The DataFrame is stored in data_busa variable.

import pandas as pd

data_bus = pd.DataFrame(
    {
        "bus": ["2x", "3Tr", "4x", "5x"],
        "bus_sale_q1": [21, 23, 25, 27],
        "bus_sale_q2": [12, 14, 16, 18],
    },
    columns=["bus", "bus_sale_q1", "bus_sale_q2"],
)
print(data_bus)

Output:

     bus    bus_sale_q1    bus_sale_q2
0    2x     21             12
1    3Tr    23             14
2    4x     25             16
3    5x     27             18

The above output shows the simple DataFrame created. After that, groupby()the total column is grouped based on the sum of sales q1 and q2 using the function.

grouped_data = data_bus.groupby(by="bus").agg("sum")
grouped_data

Output:

bus    bus_sale_q1    bus_sale_q2
2x     21             12
3Tr    23             14
4x     25             16
5x     27             18

We will use reset_index()the function to flatten the hierarchical index columns.

flat_data = grouped_data.reset_index()
flat_data

Output:

     bus    bus_sale_q1    bus_sale_q2
0    2x     21             12
1    3Tr    23             14
2    4x     25             16
3    5x     27             18

as_indexFlattening a Hierarchical Index in a Pandas Column Using

The pandas groupby()function will be used to group the bus sales data by quarter, while as_indexthe hierarchical index column of the grouped DataFrame will be flattened.

grammar:

pandas.DataFrame.groupby(by, level, axis, as_index)

in,

  • level: The column on which the groupby operation must be performed.
  • by: The column on which the groupby operation must be performed.
  • axis: Whether to split along rows (0) or columns (1).
  • as_index: For aggregate output, returns an object with indexed group labels.

We will groupby()group the bus sales data by quarter using the Pandas function with as_indexthe parameter set to False. This ensures that the hierarchical index of the grouped DataFrame is flattened.

We will use the same DataFrame as in the previous example.

example:

import pandas as pd

data_bus = pd.DataFrame(
    {
        "bus": ["2x", "3Tr", "4x", "5x"],
        "bus_sale_q1": [21, 23, 25, 27],
        "bus_sale_q2": [12, 14, 16, 18],
    },
    columns=["bus", "bus_sale_q1", "bus_sale_q2"],
)
data_bus
grouped_data = data_bus.groupby(by="bus", as_index=False).agg("sum")
print(grouped_data)

Output:

     bus    bus_sale_q1    bus_sale_q2
0    2x     21             12
1    3Tr    23             14
2    4x     25             16
3    5x     27             18

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding the installed version of Pandas

Publish Date:2025/04/12 Views:190 Category:Python

Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc

KeyError in Pandas

Publish Date:2025/04/12 Views:81 Category:Python

This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest

Grouping and Sorting in Pandas

Publish Date:2025/04/12 Views:90 Category:Python

This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies

Plotting Line Graph with Data Points in Pandas

Publish Date:2025/04/12 Views:65 Category:Python

Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize

Converting Timedelta to Int in Pandas

Publish Date:2025/04/12 Views:123 Category:Python

This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .

Pandas fill NaN values

Publish Date:2025/04/12 Views:93 Category:Python

This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values ​​with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50

Pandas Convert String to Number

Publish Date:2025/04/12 Views:147 Category:Python

This tutorial explains how to pandas.to_numeric() convert string values ​​of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :

How to Change the Data Type of a Column in Pandas

Publish Date:2025/04/12 Views:139 Category:Python

We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method

Get the first row of Dataframe Pandas

Publish Date:2025/04/12 Views:78 Category:Python

This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial