0 votes
1 view
in Python by (47.8k points)

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 a number of rows in each group. In other words, I have mean but I also would like to know how many numbers 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?

1 Answer

0 votes
by (107k points)

To get group-wise statistics for a dataframe, you can use the agg function that can take a list to apply several aggregation methods at once. Below is the code that shows how we use the agg() function:-

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...