Pandas: compute the mean of a column grouped by another column

FaCoffee

Say I have a dataframe like this:

            gender     height      weight  C
2000-01-01    male  42.849980  157.500553  1
2000-01-02    male  49.607315  177.340407  1
2000-01-03    male  56.293531  171.524640  1
2000-01-04  female  48.421077  144.251986  2
2000-01-05    male  46.556882  152.526206  2
2000-01-06  female  68.448851  168.272968  1
2000-01-07    male  70.757698  136.431469  2
2000-01-08  female  58.909500  176.499753  3
2000-01-09  female  76.435631  174.094104  3
2000-01-10    male  45.306120  177.540920  2

How could I compute the mean of the height column, grouped by column C? This would yield 3 different values: the mean of those heights with C=1, that of those with C=2, and so forth.

So far I tried this but to no avail:

df['height'].mean(groupby='C')

-> returns TypeError: mean() got an unexpected keyword argument 'groupby'

EdChum

Your syntax is incorrect, there is no groupby arg for mean, you want to groupby on the col of interest and then call mean on the column of interest:

In [11]:
df.groupby('C')['height'].mean()

Out[11]:
C
1    54.299919
2    52.760444
3    67.672566
Name: height, dtype: float64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

Find the frequent elements from a list in pandas column grouped by another column

From Java

How to calculate mean values grouped on another column in Pandas

From Dev

Create column with grouped values based on another column

From Dev

Count where Column is True, Grouped By Another in Pandas

From Dev

Compute row average by another column in Python/Pandas

From Dev

Compute delta column with Pandas

From Dev

Compute number of occurance of each value and Sum another column in Pandas

From Dev

Create a pandas column based on existing columns: conditional min of a column grouped by another column

From Dev

Pandas - Compute data from a column to another

From Dev

How to return value_counts() when grouped by another column in pandas

From Dev

Calculate mean from a column based on another column in pandas

From Dev

Complete date series grouped by another column in pandas and fill missing rows

From Dev

Make a grouped column by sum of another column with pandas

From Dev

Compute separate correlations, grouped by column value

From Dev

Average on column grouped by another column

From Dev

Taking the mean by one column and then by another in pandas

From Dev

Pandas Mean for Certain Column

From Dev

Pandas Grouping - Values as Percent of Grouped Totals Based on Another Column

From Dev

Normalize column in pandas dataframe by sum of grouped values of another column

From Dev

Pandas multi column mean

From Dev

Pandas .mean() for a column

From Dev

Pandas summing rows grouped by another column

From Dev

Divide the column by the sum grouped by another column

From Dev

Find different values in a column grouped by another column

From Dev

Compute mean value of rows that has the same column value in Pandas

From Dev

Pandas: How to find mean of a column based on duplicate rows in another column?

From Dev

Making a Count of Days Column Grouped by Another Column (pandas)

From Dev

How to add a column with given values to a Pandas dataframe, grouped by another column?

From Dev

Grouped column diff, based on another column

Related Related

  1. 1

    Find the frequent elements from a list in pandas column grouped by another column

  2. 2

    How to calculate mean values grouped on another column in Pandas

  3. 3

    Create column with grouped values based on another column

  4. 4

    Count where Column is True, Grouped By Another in Pandas

  5. 5

    Compute row average by another column in Python/Pandas

  6. 6

    Compute delta column with Pandas

  7. 7

    Compute number of occurance of each value and Sum another column in Pandas

  8. 8

    Create a pandas column based on existing columns: conditional min of a column grouped by another column

  9. 9

    Pandas - Compute data from a column to another

  10. 10

    How to return value_counts() when grouped by another column in pandas

  11. 11

    Calculate mean from a column based on another column in pandas

  12. 12

    Complete date series grouped by another column in pandas and fill missing rows

  13. 13

    Make a grouped column by sum of another column with pandas

  14. 14

    Compute separate correlations, grouped by column value

  15. 15

    Average on column grouped by another column

  16. 16

    Taking the mean by one column and then by another in pandas

  17. 17

    Pandas Mean for Certain Column

  18. 18

    Pandas Grouping - Values as Percent of Grouped Totals Based on Another Column

  19. 19

    Normalize column in pandas dataframe by sum of grouped values of another column

  20. 20

    Pandas multi column mean

  21. 21

    Pandas .mean() for a column

  22. 22

    Pandas summing rows grouped by another column

  23. 23

    Divide the column by the sum grouped by another column

  24. 24

    Find different values in a column grouped by another column

  25. 25

    Compute mean value of rows that has the same column value in Pandas

  26. 26

    Pandas: How to find mean of a column based on duplicate rows in another column?

  27. 27

    Making a Count of Days Column Grouped by Another Column (pandas)

  28. 28

    How to add a column with given values to a Pandas dataframe, grouped by another column?

  29. 29

    Grouped column diff, based on another column

HotTag

Archive