Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count of unique value in column pandas [duplicate]

I have a dataframe and I am looking at one column within the dataframe called names

array(['Katherine', 'Robert', 'Anne', nan, 'Susan', 'other'], dtype=object)

I am trying to make a call to tell me how many times each of these unique names shows up in the column, for example if there are 223 instances of Katherine etc. How do i do this? i know value_counts just shows 1 for each of these because they are the separate unique values

like image 789
kwashington122 Avatar asked Jan 15 '17 19:01

kwashington122


1 Answers

If I understand you correctly, you can use pandas.Series.value_counts.

Example:

import pandas as pd
import numpy as np

s = pd.Series(['Katherine', 'Robert', 'Anne', np.nan, 'Susan', 'other'])

s.value_counts()

Katherine    1
Robert       1
other        1
Anne         1
Susan        1
dtype: int64

The data you provided only has one of each name - so here is an example with multiple 'Katherine' entries:

s = pd.Series(['Katherine','Katherine','Katherine','Katherine', 'Robert', 'Anne', np.nan, 'Susan', 'other'])

s.value_counts()

Katherine    4
Robert       1
other        1
Anne         1
Susan        1
dtype: int64

When applied to your Dataframe you will call this as follows:

df['names'].value_counts()
like image 177
nipy Avatar answered Oct 18 '22 20:10

nipy