• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Group By Columns and Count Rows in pandas DataFrame

How to Group By Columns and Count Rows in pandas DataFrame

October 11, 2022 Leave a Comment

To group by multiple columns and then find the count of rows in a pandas DataFrame, you can use the groupby() and count() functions.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], "gender":["F","F","F","F","M","M","M","F","M"]})

print(df)
print(df.groupby(["animal_type","gender"])["gender"].count().rename('count').reset_index())

#Output:
  animal_type gender
0         dog      F
1         cat      F
2         dog      F
3         cat      F
4         dog      M
5         dog      M
6         cat      M
7         cat      F
8         dog      M

  animal_type gender  count
0         cat      F      3
1         cat      M      1
2         dog      F      2
3         dog      M      3

When working with data, it is very useful to be able to group and aggregate data by multiple columns to understand the various segments of our data.

One such case is if you want to group your data and get the row counts of the groups.

To get the counts by group of columns in a pandas DataFrame, you can use the groupby() and count() functions.

Below is a simple example showing you how you can group by and then count the rows in each group in a pandas DataFrame in Python.

In the example below, I’ve renamed the count of rows to count and then reset the index so that we can work with the resulting DataFrame easier.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], "gender":["F","F","F","F","M","M","M","F","M"]})

print(df)
print(df.groupby(["animal_type","gender"])["gender"].count().rename('count').reset_index())

#Output:
  animal_type gender
0         dog      F
1         cat      F
2         dog      F
3         cat      F
4         dog      M
5         dog      M
6         cat      M
7         cat      F
8         dog      M

  animal_type gender  count
0         cat      F      3
1         cat      M      1
2         dog      F      2
3         dog      M      3

Using groupby() and count() on Single Column in pandas DataFrame

You can use groupby() to group a pandas DataFrame by one column or multiple columns.

If you want to group a pandas DataFrame by one column and then the counts of each value in that group with count(), you can do the following.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], "gender":["F","F","F","F","M","M","M","F","M"]})

print(df)
print(df.groupby(["animal_type"])["animal_type"].count().rename('count').reset_index())

#Output:
  animal_type gender
0         dog      F
1         cat      F
2         dog      F
3         cat      F
4         dog      M
5         dog      M
6         cat      M
7         cat      F
8         dog      M

  animal_type  count
0         cat      4
1         dog      5

Using groupby() to Group By Multiple Columns and count() in pandas DataFrame

If you want to group a pandas DataFrame by multiple columns and then the counts of each value in each group with count(), you can do the following.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], "gender":["F","F","F","F","M","M","M","F","M"]})

print(df)
print(df.groupby(["animal_type","gender"])["gender"].count().rename('count').reset_index())

#Output:
  animal_type gender
0         dog      F
1         cat      F
2         dog      F
3         cat      F
4         dog      M
5         dog      M
6         cat      M
7         cat      F
8         dog      M

  animal_type gender  count
0         cat      F      3
1         cat      M      1
2         dog      F      2
3         dog      M      3

Hopefully this article has been useful for you to learn how to group by and count in pandas with groupby() and count().

Other Articles You'll Also Like:

  • 1.  Using Python to Capitalize Every Other Letter of String
  • 2.  pandas T Function – Transposing DataFrames with pandas
  • 3.  Using Python to Remove Quotes from String
  • 4.  Perfect Numbers in Python
  • 5.  pandas dropna – Drop Rows or Columns with NaN in DataFrame
  • 6.  Python power function – Exponentiate Numbers with math.pow()
  • 7.  Using Python to Split String by Newline
  • 8.  Convert String to Integer with int() in Python
  • 9.  Check if Variable is None in Python
  • 10.  Ceiling Division in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy