Built with Tutify.io

    AIStudyLabs

    Pandas Drop Level From Multi-Level Column Index

    Pandas Drop Level From Multi-Level Column Index

    By using DataFrame.droplevel() or DataFrame.columns.droplevel() you can drop a level from multi-level column index from pandas DataFrame. Note that when you have multi-index columns, DataFrame.columns return pandas.Multiindex.

    A multi-level index DataFrame is a type of DataFrame that contains multiple level or hierarchical indexing. Dropping a level of a multi-level column index in a pandas DataFrame removes the entire column level.

    Key Points –

    <a id="h-1-quick-examples-of-drop-level-from-columns"></a>

    1. Quick Examples of Drop Level From Columns

    Below are some most used examples of how to drop level from multi-level column index from pandas DataFrame.

    
    # Quick examples of drop level from columns
    
    # Using droplevel() 
    # To drop level from multi level column labels
    df = df.droplevel(0, axis=1) 
    
    # Drop first level of column label
    df.columns = df.columns.droplevel(0)
    
    # Alternate way
    df.columns = [col[1] for col in df.columns]
    
    # By assigning new columns
    df.columns = ['Toyoto', 'Ford','Tesla','Nio']
    
    # Drops second level
    df.columns = df.columns.droplevel(1)
    
    # Merge on level name with another level
    df.columns = ['_'.join(col) for col in df.columns]
    

    Now let's see the syntax of these methods, execute with some test data and validate results.

    
    import pandas as pd
    new_index = pd.MultiIndex.from_tuples([("r0", "rA"),
                                           ("r1", "rB")],
                                           names=['Courses','Fee'])
    
    cols = pd.MultiIndex.from_tuples([("Gasoline", "Toyoto"), 
                                      ("Gasoline", "Ford"), 
                                      ("Electric", "Tesla"),
                                      ("Electric", "Nio")])
    data=[[100,300, 900,400 ], [200,500, 300,600]]
    
    df = pd.DataFrame(data, columns=cols,index=new_index)
    print(df)
    
    # Output:
    #            Gasoline      Electric     
    #              Toyoto Ford    Tesla  Nio
    # Courses Fee                            
    # r0      rA       100  300      900  400
    # r1      rB       200  500      300  600
    

    <a id="h-2-pandas-dataframe-droplevel-syntax-amp-examples"></a>

    2. Pandas DataFrame.droplevel() Syntax & Examples

    Below is the syntax of the DataFrame.droplevel() method.

    
    # DataFrame.droplevel() Syntax
    DataFrame.droplevel(level, axis=0)
    

    <a id="h-3-drop-a-level-from-multi-level-column-index-in-pandas"></a>

    3. Drop a Level From Multi-Level Column Index in Pandas

    Use DataFrame.droplevel() to drop single or more levels from multi-level column labels. Multi-level columns are used when you wanted to group columns together. This droplevel() method can be used to drop levels from columns and rows. Use axis=1 param to drop columns. To drop row-level use axis=0

    
    # Using droplevel() 
    # To drop level from multi level column labels
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df = df.droplevel(0, axis=1) 
    print(df)
    

    Yields below output.

    
    # Output:
                 Toyoto  Ford  Tesla  Nio
    Courses Fee                          
    r0      rA      100   300    900  400
    r1      rB      200   500    300  600
    

    <a id="h-4-drop-level-using-multiindex-droplevel"></a>

    4. Drop Level Using MultiIndex.droplevel()

    You can also use pandas.MultiIndex.droplevel() to drop columns level. When you have Multi-level columns DataFrame.columns return MultiIndex object and use droplevel() on this object to drop level.

    
    # Drop first level of column label
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = df.columns.droplevel(0)
    print(df)
    

    Yields the same output as above.

    <a id="h-5-alternate-ways-to-drop-level-from-multi-level-columns"></a>

    5. Alternate Ways to Drop Level From Multi Level Columns

    Below are some other alternative ways to drop levels from Multi-Level Columns of pandas DataFrame.

    
    # Alternate ways to drop level from
    # multilevel columns
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = [col[1] for col in df.columns]
    print(df)
    

    By assigning new columns. In order to use this, you need to know all column names from DataFrame.

    
    # By assigning new columns
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = ['Toyoto', 'Ford','Tesla','Nio']
    print(df)
    

    In case if you wanted to prefix the level you are dropping to the next level, use the below approach.

    
    # Drop first level and add level label as prefix
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = ['_'.join(col) for col in df.columns]
    print(df)
    

    Yields below output

    
    # Output:
                 Gasoline_Toyoto  Gasoline_Ford  Electric_Tesla  Electric_Nio
    Courses Fee                                                              
    r0      rA               100            300             900           400
    r1      rB               200            500             300           600
    

    <a id="h-5-drop-two-levels-using-multi-level-column-index"></a>

    5. Drop Two Levels Using Multi-Level Column Index

    If you have multiple levels (3 or more), you may need to drop multiple levels, you can do this by chaining droplevel().

    
    # Drop two levels from multi-level column index
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = df.columns.droplevel(0)
    df.columns = df.columns.droplevel(1)
    print(df)
    

    <a id="h-6-drop-level-from-multi-index-row"></a>

    6. Drop level from multi-index Row

    To drop a level from a multi-index row in a Pandas DataFrame, you can use the droplevel() method. If you have multiple indexes, you can use the below examples to drop one index.

    
    # Drop level from index
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.index = df.index.droplevel(1) 
    print(df)
    
    # Using droplevel() for row index
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df=df.droplevel(0, axis=0) 
    print(df)
    

    Here,

    
    # Output:
        Gasoline      Electric     
          Toyoto Ford    Tesla  Nio
    Fee                            
    rA       100  300      900  400
    rB       200  500      300  600
    

    <a id="h-7-complete-example-for-drop-multi-level-column-index"></a>

    7. Complete Example For Drop Multi-Level Column Index

    
    import pandas as pd
    new_index = pd.MultiIndex.from_tuples([("r0", "rA"),
                                           ("r1", "rB")],
                                           names=['Courses','Fee'])
    
    cols = pd.MultiIndex.from_tuples([("Gasoline", "Toyoto"), 
                                      ("Gasoline", "Ford"), 
                                      ("Electric", "Tesla"),
                                      ("Electric", "Nio")])
    data=[[100,300, 900,400 ], [200,500, 300,600]]
    
    df = pd.DataFrame(data, columns=cols,index=new_index)
    print(df)
    
    # Using droplevel() To drop level From Multi Level Column Labels
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df=df.droplevel(0, axis=1) 
    print(df)
    
    # Drop First Level of Column Label
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = df.columns.droplevel(0)
    print(df)
    
    # Alternate ways
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = [col[1] for col in df.columns]
    print(df)
    
    # Another Alternate way
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = ['Toyoto', 'Ford','Tesla','Nio']
    print(df)
    
    # Drop second Level
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = df.columns.droplevel(1)
    print(df)
    
    # Drop First Level and add level label as prefix
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.columns = ['_'.join(col) for col in df.columns]
    print(df)
    
    # Drop Level From Index
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df.index = df.index.droplevel(1) 
    print(df)
    
    # Using droplevel() For Row index
    df = pd.DataFrame(data, columns=cols,index=new_index)
    df=df.droplevel(0, axis=0) 
    print(df)
    

    <a id="h-frequently-asked-questions-on-drop-level-from-multi-level-column-index"></a>

    Frequently Asked Questions on Drop Level From Multi-Level Column Index

    Dropping a level from a multi-level column index in Pandas refers to simplifying the structure of the DataFrame by removing one or more levels from the column index hierarchy.

    You can drop a level from a multi-level column index using the droplevel() method in Pandas. This method allows you to specify the level or levels you want to remove from the column index.

    Dropping a level from a multi-level column index in Pandas does not modify the original DataFrame. Instead, it returns a new DataFrame with the specified level(s) removed. This ensures that the original DataFrame remains unchanged, maintaining data integrity and allowing for reproducibility in data processing pipelines.

    Dropping levels from a multi-level column index can simplify data manipulation and analysis, making it easier to work with the DataFrame, especially when dealing with complex hierarchical data structures. It can also improve readability and reduce complexity in data processing tasks.

    You can drop multiple levels from a multi-level column index at once in Pandas by passing a list of levels to the level parameter of the droplevel() method. For example, level=[0, 1] indicates that you want to drop both the first and second levels from the column index, resulting in a DataFrame with a simplified column index containing only one level.

    There are alternative methods such as directly assigning column labels or using other DataFrame manipulation techniques. However, using droplevel() is often the most straightforward and explicit way to drop levels from a multi-level column index.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned how to drop level from multi-level column index in pandas DataFrame using DataFrame.columns.droplevel() and pandas.MultiIndex.droplevel() method.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References