Pandas DataFrame ‘value_count()’ Method.


Pandas DataFrame value_count() Method.

Table Of Contents:

  1. Syntax ‘value_count( )’ Method In Pandas.
  2. Examples ‘value_count( )’ Method.

(1) Syntax:

DataFrame.value_counts(subset=None, normalize=False, sort=True, ascending=False, dropna=True

Description:

  • Return a Series containing counts of unique rows in the DataFrame.
  • df['your_column'].value_counts() – this will return the count of unique occurences in the specified column.
  • It is important to note that value_counts only works on pandas series, not Pandas dataframes. As a result, we only include one bracket df[‘your_column’] and not two brackets df[[‘your_column’]].

Parameters:

  • subset: list-like, optional – Columns to use when counting unique combinations.
  • normalize: bool, default False – Return proportions rather than frequencies.
  • sort: bool, default True – Sort by frequencies.
  • ascending: bool, default False – Sort in ascending order.
  • dropna: bool, default True – Don’t include counts of rows that contain NA values.

(2) Examples Of value_count() Method:

Example-1

import pandas as pd
student = {'Name':['Subrat','Abhispa','Arpita','Anuradha','Namita'],
          'Roll_No':[100,101,102,103,104],
          'Subject':['Math','English','Science','History','Commerce'],
          'Mark':[95,88,76,73,93],
          'Gender':['Male','Female','Female','Female','Female']}
student_object = pd.DataFrame(student)
student_object

Output:

# Value Counts On DataFrame.

student_object.value_counts()

Output:

Name      Roll_No  Subject   Mark  Gender
Abhispa   101      English   88    Female    1
Anuradha  103      History   73    Female    1
Arpita    102      Science   76    Female    1
Namita    104      Commerce  93    Female    1
Subrat    100      Math      95    Male      1
dtype: int64

Note:

  • value_count() on a DataFrame will result you the count of unique rows of the DataFrame.

# Value Counts On DataFrame Column.

student_object['Gender'].value_counts()

Output:

Female    4
Male      1
Name: Gender, dtype: int64

Leave a Reply

Your email address will not be published. Required fields are marked *