- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Write a program in Python to count the records based on the designation in a given DataFrame
Input −
Assume, we have a DataFrame and group the records based on the designation is −
Designation architect 1 programmer 2 scientist 2
Solution
To solve this, we will follow the below approaches.
Define a DataFrame
Apply groupby method for Designation column and calculate the count as defined below,
df.groupby(['Designation']).count()
Example
Let us see the following implementation to get a better understanding.
import pandas as pd data = { 'Id':[1,2,3,4,5], 'Designation': ['architect','scientist','programmer','scientist','programmer']} df = pd.DataFrame(data) print("DataFrame is\n",df) print("groupby based on designation:") print(df.groupby(['Designation']).count())
Output
Designation architect 1 programmer 2 scientist 2
Advertisements