Groupby multiple columns count size and calculate mean of another column in Pandas

ah bon

I have a dataframe as follows:

   index    col1    col2   col3  col4                           col5
0      0  Week_1   James   John     1                  when and why?
1      1  Week_1   James   John     3             when and why? How?
2      2  Week_2   James   John     2  How far is it? Are you going?
3      3  Week_2    Mark    Jim     3              Do you know when?
4      4  Week_2  Andrew  Simon     3                     What time?
5      5  Week_2  Andrew  Simon     6                     What time?

How could I groupby col2 and col3 then calculate mean and count numbers of col2 and col3?

df.groupby(['col2','col3'], as_index=False).agg({'col4':'mean'}).reset_index()

Output:

   index    col2   col3  col4
0      0  Andrew  Simon   4.5
1      1   James   John   2.0
2      2    Mark    Jim   3.0

df.groupby(['col2','col3']).size().reset_index()

Output:

     col2   col3  0
0  Andrew  Simon  2
1   James   John  3
2    Mark    Jim  1

How could I get result like this? Thanks.

   index    col2   col3  mean  count
0      0   James   John   2.0      3
1      3    Mark    Jim   3.0      1
2      4  Andrew  Simon   4.5      2
Allen

You can use groupby and agg(may need to use pandas 0.25+).

(
    df.groupby(['col2','col3'])
    .agg(index=('index', 'first'),
         mean=('col4', 'mean'),
         count=('col4', 'size'))
    .reset_index()
    .sort_values(by='index')
)

    col2    col3    index   mean    count
1   James   John    0       2.0     3
2   Mark    Jim     3       3.0     1
0   Andrew  Simon   4       4.5     2   

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas/Python groupby and then calculate mean for another column within each group

From Dev

Pandas- Groupby multiple columns and mean from a single column

From Dev

Groupby year and calculate the average and count the size in pandas

From Dev

pandas: groupby multiple columns, concatenating one column while adding another

From Dev

PANDAS groupby 2 columns then count and mean

From Java

Calculate new column as the mean of other columns pandas

From Dev

Groupby count on multiple condition and multiple columns pandas

From Dev

Pandas Rolling mean based on groupby multiple columns

From Dev

Pandas Groupby mean and first of multiple columns

From Dev

Pandas fillna from mean with groupby for multiple columns

From Dev

Calculate mean of columns from multiple dataframe in pandas

From Python

Is there a way to count and calculate mean for text columns using groupby?

From Dev

Python: groupby multiple columns and generate count column

From Dev

Pandas groupby multiple columns, count, and resample

From Dev

using pandas groupby apply sum & count for two categorical columns based on another categorical column

From Dev

Groupby count of non NaN of another column and a specific calculation of the same columns in pandas

From Dev

write a function to groupby year and calculate the average and count the size in pandas

From Dev

Pandas DataFrame GroupBy by several columns with mean, StdDev and count statistics

From Dev

Calculate mean from a column based on another column in pandas

From Dev

Calculate difference between successive date column with groupby on another column in pandas?

From Dev

Count distinct multiple columns for each another column

From Dev

Pandas - groupby one column and get mean of all other columns

From Dev

Python Pandas groupby and mean/stdev all columns into one column

From Dev

Create new rolling mean column with GroupBy on multiple columns

From Dev

Groupby and calculate count and means based on multiple conditions in Pandas

From Java

How to calculate mean values grouped on another column in Pandas

From Dev

Pandas - Calculate mean of a selection of cells from another column

From Java

Pandas Groupby: Count and mean combined

From Dev

pandas groupby count and then conditional mean

Related Related

  1. 1

    Pandas/Python groupby and then calculate mean for another column within each group

  2. 2

    Pandas- Groupby multiple columns and mean from a single column

  3. 3

    Groupby year and calculate the average and count the size in pandas

  4. 4

    pandas: groupby multiple columns, concatenating one column while adding another

  5. 5

    PANDAS groupby 2 columns then count and mean

  6. 6

    Calculate new column as the mean of other columns pandas

  7. 7

    Groupby count on multiple condition and multiple columns pandas

  8. 8

    Pandas Rolling mean based on groupby multiple columns

  9. 9

    Pandas Groupby mean and first of multiple columns

  10. 10

    Pandas fillna from mean with groupby for multiple columns

  11. 11

    Calculate mean of columns from multiple dataframe in pandas

  12. 12

    Is there a way to count and calculate mean for text columns using groupby?

  13. 13

    Python: groupby multiple columns and generate count column

  14. 14

    Pandas groupby multiple columns, count, and resample

  15. 15

    using pandas groupby apply sum & count for two categorical columns based on another categorical column

  16. 16

    Groupby count of non NaN of another column and a specific calculation of the same columns in pandas

  17. 17

    write a function to groupby year and calculate the average and count the size in pandas

  18. 18

    Pandas DataFrame GroupBy by several columns with mean, StdDev and count statistics

  19. 19

    Calculate mean from a column based on another column in pandas

  20. 20

    Calculate difference between successive date column with groupby on another column in pandas?

  21. 21

    Count distinct multiple columns for each another column

  22. 22

    Pandas - groupby one column and get mean of all other columns

  23. 23

    Python Pandas groupby and mean/stdev all columns into one column

  24. 24

    Create new rolling mean column with GroupBy on multiple columns

  25. 25

    Groupby and calculate count and means based on multiple conditions in Pandas

  26. 26

    How to calculate mean values grouped on another column in Pandas

  27. 27

    Pandas - Calculate mean of a selection of cells from another column

  28. 28

    Pandas Groupby: Count and mean combined

  29. 29

    pandas groupby count and then conditional mean

HotTag

Archive