- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Groupby values count on the Pandas DataFrame
To Groupby value counts, use the groupby(), size() and unstack() methods of the Pandas DataFrame. At first, create a DataFrame with 3 columns −
dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'],'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'],'Quantity': [10, 50, 10, 20, 25, 50]})
Now, groupby values count with groupby() method. For count, use the size() and unstack(). The unstack() gives a new level of column labels −
dataFrame = dataFrame.groupby(['Product Category', 'Product Name', 'Quantity']).size().unstack(fill_value=0)
Example
Following is the complete code −
import pandas as pd # create a dataframe with 3 columns dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'],'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'],'Quantity': [10, 50, 10, 20, 25, 50]}) # dataframe print"Dataframe...\n",dataFrame # count and unstack dataFrame = dataFrame.groupby(['Product Category', 'Product Name', 'Quantity']).size().unstack(fill_value=0) print"\nResultant DataFrame...\n",dataFrame
Output
This will produce the following output −
Dataframe... Product Category Product Name Quantity 0 Computer Keyboard 10 1 Mobile Phone Charger 50 2 Electronics SmartTV 10 3 Electronics Camera 20 4 Computer Graphic Card 25 5 Mobile Phone Earphone 50 Resultant DataFrame... Quantity 10 20 25 50 Product Category Product Name Computer Graphic Card 0 0 1 0 Keyboard 1 0 0 0 Electronics Camera 0 1 0 0 SmartTV 1 0 0 0 Mobile Phone Charger 0 0 0 1 Earphone 0 0 0 1
- Related Articles
- Pandas GroupBy – Count the occurrences of each combination
- How to do groupby on a multiindex in Pandas?
- How to count the NaN values in a column in a Python Pandas DataFrame?
- Python - Calculate the count of column values of a Pandas DataFrame
- How to get the row count of a Pandas DataFrame?
- How to count frequency of itemsets in Pandas DataFrame
- Python - How to reset index after Groupby pandas?
- Python - Sum negative and positive values using GroupBy in Pandas
- Select rows from a Pandas DataFrame based on column values
- Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
- Python Pandas – How to select DataFrame rows on the basis of conditions
- How to apply the aggregation list on every group of pandas DataFrame?
- Python Pandas – Count the rows and columns in a DataFrame
- How to do Fuzzy Matching on Pandas Dataframe Column Using Python?
- Python - Replace values of a DataFrame with the value of another DataFrame in Pandas

Advertisements