To get the total number of elements in a pandas DataFrame after grouping, we can use the pandas DataFrame groupby size() function .

grouped_data = df.groupby(["Column1"])
grouped_data.size()  # Returns number of elements in each group in the grouped DataFrame

When working with data, it is useful for us to be able to find the number of elements in our data. It is also helpful to be able to group data and analyze statistics of the various groups.

When working with pandas DataFrames, we can find the total number of elements in a DataFrame of a grouped dataset with the pandas DataFrame size() property.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Group': ['A','A','B','B','C','A'], 
      'Sub-Group': ['1','2','1','2','1','3'],
      'Age': [43,23,71,49,52,37], 
      'Test_Score':[90,87,92,96,84,79]})

print(df)
# Output: 
  Group Sub-Group  Age  Test_Score
0     A         1   43          90
1     A         2   23          87
2     B         1   71          92
3     B         2   49          96
4     C         1   52          84
5     A         3   37          79

First, let’s group by the column “Group”. Then let’s calculate the size of this new grouped dataset. To get the size of the grouped DataFrame, we call the pandas groupby size() function in the following Python code.

grouped_data = df.groupby(["Group"]).size()

# Output:
Group
A    3
B    2
C    1
dtype: int64

Finding the Total Number of Elements in Each Group with Size() Function

Another example we can look at is if we group by multiple columns.

Let’s say we have the same DataFrame from above. If we group by both “Group” and “Sub-Group”, we can get the size for all groups again easily in the following Python code.

grouped_data = df.groupby(["Group","Sub-Group"]).size()
print(grouped_data.size())

Group  Sub-Group
A      1            1
       2            1
       3            1
B      1            1
       2            1
C      1            1
dtype: int64

The groupby size() function is very useful for understanding the distribution of your data in each group.

Hopefully this article has been helpful for you to understand how to find the size of a grouped DataFrame in pandas.

Categorized in:

Python,

Last Update: March 20, 2024