Built with Tutify.io

    AIStudyLabs

    Pandas Pivot Table Explained with Examples

    Pandas Pivot Table Explained with Examples

    What is a pivot table and how to create it in Pandas? Pandas pivot_table() function is used to make a spreadsheet-style pivot table from a given DataFrame. Using a pivot table we can analyze the data very quickly and it can give more flexibility to make an excel sheet form of a given DataFrame. It can be aggregated in a numeric column as a cross-tabulation against two categorical columns.

    In this article, I will explain how to create pivot tables in pandas and understand their syntax and parameters with examples. Creating a pivot table is a process of grouping, summarising, aggregating, and calculating statistics about the specified data in a DataFrame.

    Key Points –

    <a id="h-quick-examples-of-pivot-table"></a>

    Quick Examples of Pivot Table

    Below are quick examples of Pandas pivot table.

    
    # Quick examples of pandas pivot table
    
    # Example 1 : Create a pivot table using index
    p_table = pd.pivot_table(df, index=['Gender']) 
    
    # Example 2 : Create a pivot table using multiple index
    p_table = pd.pivot_table(df, index=['Gender', 'Category']) 
    
    # Example 3 : Create pivot table using multiple aggfunc
    p_table = pd.pivot_table(df, index=['Gender'], aggfunc= {'Fee': 'mean', 'Discount': 'sum'})
    
    # Example 4 : Create table using value parameter
    p_table = pd.pivot_table(df, index = ['Gender'], values = ['Discount'], aggfunc = 'mean' )
    
    # Example 5 : Create table using column parameter
    p_table = pd.pivot_table(df, index= ['Gender'], columns = ['Fee'], values=['Discount'], aggfunc = 'mean' )
    
    # Example 6 : Fill the NaN value using fill_value
    p_table = pd.pivot_table(df, index= ['Gender'], columns = ['Fee'], values=['Discount'], aggfunc = 'mean', fill_value = 0 )
    

    <a id="h-pandas-pivot-table-introduction"></a>

    Pandas.pivot_table() Introduction

    Following is the syntax of the Pandas.pivot_table().

    
    # Syntax of Pandas pivot table
    pandas.pivot_table(data, values=None, index=None, columns=None, aggfunc=’mean’, fill_value=None, margins=False, dropna=True, margins_name=’All’, observed=False)
    
    # Another Syntax
    DataFrame.pivot(index=None, columns=None, values=None)
    

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

    Parameters

    <a id="h-return-value"></a>

    Return Value

    It returns a DataFrame as an Excel-style pivot table.

    To run some examples of pandas pivot table, let's create DataFrame from Python dictionary in which keys are Courses, Fee, and Discount, and values are taken as a list of corresponding key values.

    
    import pandas as pd
    # Create a DataFrame
    df = pd.DataFrame({'Student Names' : ['Jenny', 'Singh', 'Charles', 'Richard', 'Veena'],
                       'Category' : ['Online', 'Offline', 'Offline', 'Offline', 'Online'],
                       'Gender' : ['Female', 'Male', 'Male', 'Male', 'Female'],
                      'Courses': ['Java', 'Spark', 'PySpark','Hadoop','C'],
                       'Fee': [15000, 17000, 27000, 29000, 12000],
                       'Discount': [1100, 800, 1000, 1600, 600]})
    print("Create DataFrame:\n",df)
    

    Yields below output.

    <a id="h-create-pandas-pivot-table"></a>

    Create Pandas Pivot Table

    <a id="h-group-the-data-into-pivot-table-using-index-param"></a>

    Group the Data into Pivot Table using Index Param

    To group the data into a pivot table using the index parameter, you specify the column(s) you want to use as the index in the resulting pivot table. For instance, the pd.pivot_table() function creates the pivot table. By specifying index=Gender, we instruct Pandas to use the Gender column as the index for grouping the data. The resulting pivot table p_table will have rows grouped by gender, and statistics will be computed for other numerical columns.

    
    # Create a pivot table using index
    p_table = pd.pivot_table(df, index=['Gender']) 
    print(p_table)
    
    # Output:
    #  Output:
    #           Discount           Fee
    # Gender                           
    # Female   850.000000  13500.000000
    # Male    1133.333333  24333.333333
    

    Note that by default pivot_table() groups on specified columns and performs the aggregation on all numeric columns. In our example DataFrame, we have Discount and Fee are numeric columns. Here, we have made a basic pivot table in pandas.

    <a id="h-create-pivot-table-using-multiple-index"></a>

    Create Pivot Table Using Multiple Index

    Using the list of features as an index to group the data. This will give more comfort to finding data in the resultant table.

    
    # Create a pivot table using multiple index
    p_table = pd.pivot_table(df, index=['Gender', 'Category'])                     
    print(p_table)
    
    # Output:
    #                    Discount           Fee
    # Gender Category                           
    # Female Online     850.000000  13500.000000
    # Male   Offline   1133.333333  24333.333333
    

    <a id="h-create-pivot-table-using-multiple-aggfunc"></a>

    Create Pivot Table Using Multiple aggfunc

    The aggfunc parameter in the pd.pivot_table() function allows you to specify the aggregation functions to be applied when summarizing the data. By default, if you don't specify any aggregation function, Pandas will use the numpy.mean() function for numerical columns.

    To use multiple aggregation functions in a pivot table, you can specify a dictionary where the keys are the columns to aggregate, and the values are lists of aggregation functions.

    
    # Create pivot table using multiple aggfunc
    p_table = pd.pivot_table(df, index=['Gender'], aggfunc= {'Fee': 'mean', 'Discount': 'sum'})
    print(p_table)
    
    # Output:
    #          Discount          Fee
    # Gender                        
    # Female      1700  13500.000000
    # Male        3400  24333.333333
    

    <a id="h-create-table-with-values-parameter"></a>

    Create Table with Values Parameter

    The value parameter is used to specify an aggregated column. When not specify this parameter, the function will aggregate all the numerical columns of the dataset (as shown above). The below example selects the columns you wanted on the result.

    
    # Create table using value parameter
    p_table = pd.pivot_table(df, index = ['Gender'], values = ['Discount'], aggfunc = 'mean' )
    print(p_table)
    
    # Output:
    #           Discount
    # Gender             
    # Female   850.000000
    # Male    1133.333333
    

    <a id="h-create-pandas-pivot-table-using-column-param"></a>

    Create Pandas Pivot Table Using Column Param

    As we can see from the above we have used multiple features as indexes, but using features as the column is given a better understanding of the resultant table than using features as an index.

    
    # Create table using column parameter
    p_table = pd.pivot_table(df, index= ['Gender'], columns = ['Fee'], values=['Discount'], aggfunc = 'mean' )
    print(p_table)
    
    # Output:
    #           Discount                               
    # Fee       12000   15000  17000   27000   29000
    # Gender                                        
    # Female    600.0  1100.0    NaN     NaN     NaN
    # Male        NaN     NaN  800.0  1000.0  1600.0
    

    <a id="h-fill-the-missing-data-in-pandas-pivot-table"></a>

    Fill the Missing Data in Pandas Pivot Table

    Using the fill_value parameter we can fill the Nan values with the specific value provided by the user in a pivot table. It defines scalar or None.

    
    # Fill the NaN value using fill_value
    p_table = pd.pivot_table(df, index= ['Gender'], columns = ['Fee'], values=['Discount'], aggfunc = 'mean', fill_value = 0 )
    print(p_table)
    
    # Output:
    #        Discount                        
    # Fee       12000 15000 17000 27000 29000
    # Gender                                 
    # Female      600  1100     0     0     0
    # Male          0     0   800  1000  1600
    

    <a id="h-faq-on-pivot-table"></a>

    FAQ on Pivot Table

    A Pivot Table in Pandas is a powerful data manipulation tool that allows you to reorganize and summarize tabular data. It allows you to reshape and aggregate data based on one or more key columns.

    To create a Pivot Table with subtotals and grand totals, you can use the margins parameter in the pivot_table function. Setting margins to True will add row and column subtotals as well as grand totals.

    You can apply custom aggregation functions in a Pivot Table by defining your custom function and passing it to the aggfunc parameter in the pivot_table function.

    You can filter data before creating a Pivot Table by using boolean indexing to select the relevant rows based on your filtering criteria.

    You can customize the names of the subtotal and grand total columns or rows in a Pandas Pivot Table by using the margins_name parameter. The margins_name parameter allows you to specify the name for the subtotal and grand total rows or columns.

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

    Conclusion

    In this article, you have learned Pandas pivot_table() function and using this how to create a pivot table in the form of Excel sheets with well-explained examples.

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

    Related Articles

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

    References