Pandas groupby apply custom function

When using the groupby function in pandas, you can apply a custom function to each group using the apply method. This allows you to perform complex operations or calculations on each group of data.

Here’s an example of how to use the groupby function and apply a custom function:


    import pandas as pd

    # Creating a sample DataFrame
    data = {
        'Category': ['A', 'A', 'B', 'B', 'A', 'B'],
        'Value': [1, 2, 3, 4, 5, 6]
    }
    df = pd.DataFrame(data)

    # Define the custom function
    def custom_function(group):
        # Perform some calculations on the group
        total = group['Value'].sum()
        average = group['Value'].mean()
        
        # Create a new DataFrame with the results
        result = pd.DataFrame({
            'Total': [total],
            'Average': [average]
        })
        
        return result

    # Apply the custom function to each group
    result = df.groupby('Category').apply(custom_function)

    print(result)
  

In this example, we have a DataFrame with two columns: ‘Category’ and ‘Value’. We want to group the data by the ‘Category’ column and apply a custom function to each group. The custom function calculates the total and average of the ‘Value’ column for each group.

The groupby function is used to group the data by the ‘Category’ column. Then, the apply method is called on the groupby object, passing the custom function as an argument. The custom function receives each group as input and returns a new DataFrame with the calculated results.

The resulting DataFrame will have the calculated totals and averages for each group:


        Total  Average
    A      8      4.0
    B     13      4.333333
  

Leave a comment