Coding Ref

How to groupby mean in Pnadas

How to groupby mean in Pnadas

In Pandas, the GroupBy.mean method is used to compute the mean of groups within a DataFrame.

This method is called on a DataFrameGroupBy object and returns a new DataFrame object containing the mean of each group.

Example

For example, consider the following DataFrame:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 1, 2, 3],
    'B': [10, 20, 30, 10, 20, 30],
    'C': [100, 200, 300, 100, 200, 300]
})

This DataFrame has three columns A, B, and C, with six rows of data.

To compute the mean of each group in this DataFrame, you would first need to use the groupby method to group the rows by a specific column or columns.

For example, to group the rows by column A, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 1, 2, 3],
    'B': [10, 20, 30, 10, 20, 30],
    'C': [100, 200, 300, 100, 200, 300]
})

# Group the rows by column A
grouped = df.groupby('A')

In the code above, the groupby method is applied to the DataFrame and the A column is specified as the grouping key.

This returns a DataFrameGroupBy object that can be used to apply various aggregation functions to the groups.

Once the rows have been grouped, you can use the mean method to compute the mean of each group.

For example:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 1, 2, 3],
    'B': [10, 20, 30, 10, 20, 30],
    'C': [100, 200, 300, 100, 200, 300]
})

# Group the rows by column A
grouped = df.groupby('A')

# Compute the mean of each group
mean_values = grouped.mean()

# Print the mean values
print(mean_values)
output
A     B      C
1  10.0  100.0
2  20.0  200.0
3  30.0  300.0

In the code above, the mean method is applied to the DataFrameGroupBy object, which computes the mean of each group.

This returns a new DataFrame object containing the mean values for each group.

In this case, the resulting DataFrame has three rows, one for each group (1, 2, and 3), and three columns (A, B, and C), with the mean value for each group and column.

You'll also like

Related tutorials curated for you

    How to use qcut() in Pandas

    How to get the first row in Pandas

    How to use ewm() in Pandas

    How to use ffill() in Pandas

    How to stack two Pandas DataFrames

    How to convert string to float in Pandas

    How to use astype() in Pandas

    How to filter a Pandas DataFrame

    How to reorder columns in Pandas

    How to find the minimum in Pandas

    How to groupby, then sort within groups in Pandas

    What is insert() in Pandas?