Python Forum
How to get count of each unique item in dataframe
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get count of each unique item in dataframe
#1
Hi,
I have below DataFrame:

data = {'Name':['Tom', 'nick', 'krish', 'jack'],
'Age':['Fail', 'Pass', 'Pass', 'Fail']}

I want to get the count of each unique item under column "Age"(for example here Pass & Fail). Finally summarize to:

Labels  values
Fail    2
Pass    2
I use below code, but I could get the count, but could not be able to arrange to the table as shown above.

#%%:
import pandas as pd
 
# intialise data of lists.
data = {'Name':['Tom', 'nick', 'krish', 'jack'],
        'Age':['Fail', 'Pass', 'Pass', 'Fail']}
 # Create DataFrame
df = pd.DataFrame(data)
data_cnt=df.groupby('Age').count()
Reply


Messages In This Thread
How to get count of each unique item in dataframe - by SriMekala - Aug-03-2019, 06:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 4,537 Oct-13-2018, 07:04 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020