Friday, November 22, 2024
Google search engine
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

Recent Comments