Lesson 14 · Mastering Pandas
How to Change Data Types and Convert Units in Pandas for Accurate Data Analysis
In this lesson, you will learn practical ways to change data types and convert measurement units in pandas DataFrames. This skill is essential for cleaning…
- CourseMastering Pandas
- Lesson14 of 44
- Video21 min
- FormatJupyter notebook · 19 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbChanging Data Types and Converting Units with Pandas#
In this lesson, you will learn practical ways to change data types and convert measurement units in pandas DataFrames. This skill is essential for cleaning real-world data, fixing mismatches, and preparing for analysis.
Let us explore why and how to do these conversions, using the Titanic dataset as our guide.
import warnings; warnings.filterwarnings('ignore')
# Data setup (Titanic Dataset)
import pandas as pd
import numpy as np
np.random.seed(42)
url = 'https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'
df = pd.read_csv(url)
print(df.shape)
print(df.head(3))
Why do data types matter?#
Each column in a DataFrame can store numbers, text, dates, or categories. If a column has the wrong type, your analysis and calculations might be wrong or throw errors. Let us check the data types of our Titanic dataset.
# View data types of each column
print(df.dtypes)
# Check if 'Age' column has any missing values
print(df['Age'].isnull().sum())
# Convert Age to integer (if possible)
df['Age_int'] = df['Age'].fillna(-1).astype(int)
print(df[['Age', 'Age_int']].head(8))
# Convert 'Sex' column to a category for space and speed
df['Sex_cat'] = df['Sex'].astype('category')
print(df['Sex_cat'].dtype)
print(df['Sex_cat'].head())
# Convert 'Pclass' to category, since there are only 3 ticket classes
df['Pclass'] = df['Pclass'].astype('category')
print(df['Pclass'].dtype)
print(df['Pclass'].unique())
# Convert 'Cabin' column to string type (new in pandas 1.0+)
df['Cabin_str'] = df['Cabin'].astype('string')
print(df['Cabin_str'].dtype)
print(df[['Cabin', 'Cabin_str']].head(7))
# Convert 'Fare' from British pounds to US dollars (approximate, 1 GBP = 1.25 USD)
df['Fare_usd'] = df['Fare'] * 1.25
print(df[['Fare', 'Fare_usd']].head(7))
# Convert 'Age' from years to months (super easy!)
df['Age_months'] = df['Age'] * 12
print(df[['Age', 'Age_months']].head(10))
# Convert 'Name' to uppercase (string operation)
df['Name_up'] = df['Name'].str.upper()
print(df[['Name', 'Name_up']].head(5))
# Convert 'Embarked' codes from text to category codes (for ML or stats)
df['Embarked_code'] = df['Embarked'].astype('category').cat.codes
print(df[['Embarked', 'Embarked_code']].head(8))
# Try a bulk conversion of data types with .astype()
dtype_map = {'Fare': 'float32', 'Age': 'float32', 'Survived': 'int8'}
df = df.astype(dtype_map)
print(df[['Fare', 'Age', 'Survived']].dtypes)
# Convert 'Ticket' numbers to strings, even if they look numeric
df['Ticket_str'] = df['Ticket'].astype(str)
print(df[['Ticket', 'Ticket_str']].head(6))
# Detect non-numeric values in 'Fare' (should be none, but a good habit!)
is_numeric = pd.to_numeric(df['Fare'], errors='coerce').notnull()
print('Non-numeric fares:', (~is_numeric).sum())
# Use pd.to_datetime() to convert 'Sex' (for demo, this fails!)
try:
df['Sex_dt'] = pd.to_datetime(df['Sex'])
except Exception as e:
print('Conversion failed:', e)
# Use input() to enter height in inches and convert to centimeters
inches = float(input('Enter height in inches: '))
centimeters = inches * 2.54
print(f'Height: {centimeters} cm')
# Detect and convert floats stored as strings in a new column
df['FakeFare'] = df['Fare'].astype(str)
df['FakeFare_num'] = pd.to_numeric(df['FakeFare'], errors='coerce')
print(df[['FakeFare', 'FakeFare_num']].head(7))
# Mini-project: How many adults and children? (Assume adult is 18 or older)
df['Is_adult'] = df['Age'] >= 18
print(df['Is_adult'].value_counts(dropna=False))
# What percent of each sex survived? (using type conversions)
result = df.groupby('Sex_cat')['Survived'].mean() * 100
print(result)
Recap: Data Type Changes and Unit Conversion Matter#
You have now seen how to:
- Check and change column data types
- Safely convert units for analysis
- Handle user input and errors
- Prepare data for machine learning models
These skills help make your analysis accurate and reliable.
If you enjoyed this hands-on walkthrough, please like the video and subscribe for more friendly Python and pandas lessons!
Give these techniques a try on your own data, and let us know in the comments what new problems you have solved.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



