Skip to content
Fix Code Error

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

March 13, 2021 by Code Error
Posted By: Anonymous

I have a data frame df and I use several columns from it to groupby:

df['col1','col2','col3','col4'].groupby(['col1','col2']).mean()

In the above way I almost get the table (data frame) that I need. What is missing is an additional column that contains number of rows in each group. In other words, I have mean but I also would like to know how many number were used to get these means. For example in the first group there are 8 values and in the second one 10 and so on.

In short: How do I get group-wise statistics for a dataframe?

Solution

On groupby object, the agg function can take a list to apply several aggregation methods at once. This should give you the result you need:

df[['col1', 'col2', 'col3', 'col4']].groupby(['col1', 'col2']).agg(['mean', 'count'])
Answered By: Anonymous

Related Articles

  • making matplotlib scatter plots from dataframes in…
  • Pandas pivot_table: filter on aggregate function
  • Change column type in pandas
  • Show Rows That Are Different Between Two Tables - MS Access
  • Conditional aggregation based on groups in a data frame R
  • Please help me with this Query formula:
  • SQL Server JOIN missing NULL values
  • Spark SQL: apply aggregate functions to a list of columns
  • Pyspark: Filter dataframe based on multiple conditions
  • How to sum each table of values in each index…
  • OR condition in update impacting performance
  • create a column string of ordered values based on…
  • Find non-ASCII characters in varchar columns using…
  • "Large data" workflows using pandas
  • jqGrid: Frozen columns of parent grid not working…
  • data.table vs dplyr: can one do something well the…
  • SQL query return data from multiple tables
  • Pandas: Drop rows with missing data and apply binary…
  • Pandas: Change output values from float to int from…
  • Convert categorical data in pandas dataframe
  • How do I include certain conditions in SQL Count
  • What's the best way to select the minimum value from…
  • Efficient way to apply multiple filters to pandas…
  • google sheets week/year array formula
  • Maven2: Missing artifact but jars are in place
  • Select corresponding value of not included column in…
  • Export to CSV using jQuery and html
  • Selectively suppressing legend in altair plot
  • Dataframe to pivot using pandas
  • Pandas Merging 101
  • Python: How to split each string into new row with…
  • how to slice pandas dataframe columns with default…
  • Pandas Dataframe to CSV only gets the first row
  • Merge 2 dataframes of different sizes after a…
  • Pandas DataFrame Groupby two columns and get counts
  • pandas get rows which are NOT in other dataframe
  • How do I expand the output display to see more…
  • Dataframe count set of conditions passed by several…
  • ROW_NUMBER() in MySQL
  • How to return a list from a pos tag column?
  • COUNT(*) vs. COUNT(1) vs. COUNT(pk): which is better?
  • creating dataframe using list of dictionaries
  • Add new column in Pandas DataFrame Python
  • Pandas - DF with lists - find all rows that match a…
  • how to split dataframe cells using delimiter into…
  • Access PostgreSQL hstore keys and values in Python…
  • Pandas multi-index unstack to single row
  • DROP Constraint without knowing the name
  • Alter table to modify default value of column
  • Flatten the data frame column of list containing…
  • table rows inside dom-repeat does not line up with…
  • SQL GROUP BY CASE statement with aggregate function
  • How to add multiple columns to pandas dataframe in…
  • How to get number of rows inserted by a transaction
  • pandas unique values multiple columns
  • Difference between partition key, composite key and…
  • Python Pandas - Time Series Find Index of Previous Row
  • How to save a pandas DataFrame table as a png
  • Apply multiple functions to multiple groupby columns
  • How to combine the data from two different…
  • How to change csv into dictionary
  • Overwrite css on particular column cell
  • How can I make my flexbox layout take 100% vertical space?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Calculate count of a numeric column into new columns…
  • Create a day-of-week column in a Pandas dataframe…
  • Numbering rows in pandas dataframe (with condition)
  • Importing Pandas gives error AttributeError: module…
  • How to compile a table of data in google sheets?
  • Calculate the mean by group
  • How to join dataframe?
  • What are the undocumented features and limitations…
  • Python loop and build a dict from list
  • How are iloc and loc different?
  • Merge list of results into a single variable with Python
  • How can I merge the columns from two tables into one output?
  • How to keep rows with more than three columns…
  • How to add padded rows of 0 to a pandas dataframe?
  • How to prevent scrolling the whole page?
  • How to edit `cell[i]` in data grid view C# win form…
  • Pandas create empty DataFrame with only column names
  • What is your most productive shortcut with Vim?
  • Update or Insert (multiple rows and columns) from…
  • How to Incrementally Increase from a Starting Value…
  • Memcached vs. Redis?
  • When to use single quotes, double quotes, and…
  • mongodb group values by multiple fields
  • Concatenate rows of two dataframes in pandas
  • How to create a groupby of two columns with all…
  • find duplicateds and fill column
  • PostgreSQL: ERROR: operator does not exist: integer…
  • binding backbone form view UIto model change to…
  • Aurelia Nested repeats of objects
  • Pandas counting/adding values by date and id
  • Remap values in pandas column with a dict
  • pandas read_csv index_col=None not working with…
  • How do I divide values across dataframes in pandas?
  • Pandas - Reshape a dataframe columns based on…
  • Bootstrap 3 hidden-xs makes row narrower
  • Import Python Script Into Another?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Shuffling a list of objects

Next Post:

How can I make Bootstrap columns all the same height?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error