Mathew K Analytics

Lesson 3 · Probability and Statistics in python

Understanding Data Types: Categorical, Numerical, Continuous, and Discrete Explained

In this lesson, you will learn to recognize different types of data used in statistics and data science. We will focus on four key data types: Categorical…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Welcome to Probability and Statistics: Types of Data#

In this lesson, you will learn to recognize different types of data used in statistics and data science.

We will focus on four key data types:

  • Categorical (qualitative)
  • Numerical (quantitative)
  • Discrete
  • Continuous

Let's get started!

# Let us start by importing the libraries we will use
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# These tools help you load, analyze, and display data

What are Types of Data?#

Understanding data types helps you choose the right tool or graph.

  • Categorical data: Labels or categories (like colors, city names)
  • Numerical data: Numbers you can add or average
    • Discrete: Whole numbers you count (like people in a room)
    • Continuous: Any value in a range (like height or weight)

You will see real examples soon.

# Data setup
# Let us load the Titanic dataset. It has both categorical and numerical data.
titanic = sns.load_dataset("titanic")
print("Rows, Columns:", titanic.shape)
titanic.head()
Rows, Columns: (891, 15)
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

Spotting Categorical Data#

Categorical data is made up of non-numeric labels. Examples in Titanic: sex, embarked, class, who, deck.

You CANNOT do math on these, but you can count or compare groups.

# Let us check which columns are categorical
categorical_cols = titanic.select_dtypes(include=["object", "category"]).columns
print("Categorical columns:", list(categorical_cols))
Categorical columns: ['sex', 'embarked', 'class', 'who', 'deck', 'embark_town', 'alive']

Spotting Numerical Data#

Numerical data are values you can count or measure. Examples in Titanic: age, fare, sibsp (siblings/spouses), parch (parents/children).

You CAN do math on these values.

# Let us see which columns are numerical
numerical_cols = titanic.select_dtypes(include=["int", "float"]).columns
print("Numerical columns:", list(numerical_cols))
Numerical columns: ['survived', 'pclass', 'age', 'sibsp', 'parch', 'fare']
# Let us peek at the data types for each column
titanic.dtypes
survived          int64
pclass            int64
sex              object
age             float64
sibsp             int64
parch             int64
fare            float64
embarked         object
class          category
who              object
adult_male         bool
deck           category
embark_town      object
alive            object
alone              bool
dtype: object

Discrete vs Continuous Data#

Discrete data: Whole numbers, countable items (1, 2, 3, ...). Continuous data: Any value in a range, like 2.51 or 180.6.

In Titanic:

  • sibsp, parch are discrete (number of siblings, number of children).
  • age, fare are continuous (age in years, fare in money).
# Let us count the unique values for 'sibsp' and 'parch'
print("sibsp unique values:", titanic['sibsp'].unique())
print("parch unique values:", titanic['parch'].unique())

# Let us check some age and fare values
print("Age samples:", titanic['age'].dropna().unique()[:10])
print("Fare samples:", titanic['fare'].unique()[:10])
sibsp unique values: [1 0 3 4 2 5 8]
parch unique values: [0 1 2 5 3 4 6]
Age samples: [22. 38. 26. 35. 54.  2. 27. 14.  4. 58.]
Fare samples: [ 7.25   71.2833  7.925  53.1     8.05    8.4583 51.8625 21.075  11.1333
 30.0708]

Quick Visualization: Categorical Example#

Let us see how many people traveled in each class (pclass).

This is a bar plot for categorical data.

# Plotting a bar chart for passenger class (categorical)
sns.countplot(data=titanic, x='pclass')
plt.title('Passenger Count by Class')
plt.xlabel('Class')
plt.ylabel('Number of Passengers')
plt.show()
No description has been provided for this image

Quick Visualization: Numerical Example#

Now, let us look at the ages of passengers.

A histogram is great to plot continuous numbers.

# Plotting a histogram for passenger age (numerical, continuous)
sns.histplot(data=titanic, x='age', bins=30, kde=True)
plt.title('Distribution of Passenger Ages')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()
No description has been provided for this image
# Practice: What type of data is 'sex'?
answer = input("Is 'sex' categorical, discrete, or continuous? ")
if answer.lower() == "categorical":
    print("Correct! 'sex' is categorical.")
else:
    print("Try again: Categorical means labels like male or female.")
    
Correct! 'sex' is categorical.
# Visual summary: Counts per embarkation port (categorical)
sns.countplot(data=titanic, x='embarked')
plt.title('Passengers by Embarkation Port')
plt.xlabel('Embarked Port')
plt.ylabel('Number of Passengers')
plt.show()
No description has been provided for this image
# Now, let us see how fare (continuous number) looks by class (categorical group)
sns.boxplot(data=titanic, x='pclass', y='fare')
plt.title('Fare by Passenger Class')
plt.xlabel('Class')
plt.ylabel('Fare')
plt.show()
No description has been provided for this image
# Show missing values for categorical and numerical columns
print(titanic[categorical_cols].isnull().sum())
print(titanic[numerical_cols].isnull().sum())
sex              0
embarked         2
class            0
who              0
deck           688
embark_town      2
alive            0
dtype: int64
survived      0
pclass        0
age         177
sibsp         0
parch         0
fare          0
dtype: int64

Wrap-up: Types of Data#

  • Categorical: Labels like male, female, port
  • Discrete: Counted numbers like siblings, children
  • Continuous: Measured values like age, fare

Understanding these types will help you explore and analyze any dataset.

Try it yourself!#

  • Use sns.histplot to plot another continuous variable.
  • Try counting a different categorical group with sns.countplot.

Pause and type your own examples.

If you learned something new, please like and subscribe for more!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.