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

Updated on: 24-Feb-2021

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements