patternpythonCritical
Get statistics for each group (such as count, mean, etc) using pandas GroupBy?
Viewed 0 times
pandaseachgetstatisticssuchusingcountetcforgroup
Problem
I have a dataframe
In the above way, I almost get the table (dataframe) 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 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?
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 (dataframe) 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 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'])Code Snippets
df[['col1', 'col2', 'col3', 'col4']].groupby(['col1', 'col2']).agg(['mean', 'count'])Context
Stack Overflow Q#19384532, score: 636
Revisions (0)
No revisions yet.