Thursday, October 9, 2025
HomeData Modelling & AIGetting More Value from the Pandas value_counts

Getting More Value from the Pandas value_counts

[Related Article: Data Valuation – What is Your Data Worth and How do You Value it?]

value_counts()

Syntax

Series.value_counts()

Parameters

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.value_counts.html

Basic usage

Importing the dataset

# Importing necessary librariesimport pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline# Reading in the data
train = pd.read_csv('../input/titanic/train.csv')

Explore the first few rows of the dataset

train.head()

Calculating the number of null values

train.isnull().sum()

1. value_counts() with default parameters

train['Embarked'].value_counts()
-------------------------------------------------------------------S      644
C      168
Q       77

2. value_counts() with relative frequencies of the unique values.

train['Embarked'].value_counts(normalize=True)
-------------------------------------------------------------------S    0.724409
C    0.188976
Q    0.086614

3. value_counts() in ascending order

train['Embarked'].value_counts(ascending=True)
-------------------------------------------------------------------Q     77
C    168
S    644

4. value_counts() displaying the NaN values

train['Embarked'].value_counts(dropna=False)
-------------------------------------------------------------------S      644
C      168
Q       77
NaN      2

5. value_counts() to bin continuous data into discrete intervals

# applying value_counts on a numerical column without the bin parametertrain['Fare'].value_counts()
train['Fare'].value_counts(bins=7)

[Related Article: From Pandas to Scikit-Learn — A New Exciting Workflow]


References


Originally Posted Here

RELATED ARTICLES

Most Popular

Dominic
32345 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6714 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6834 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS