DEV Community

Cover image for Casting in Python (Type Conversion)
mary kariuki
mary kariuki

Posted on

Casting in Python (Type Conversion)

Casting in Python Using Pandas (With Excel Dataset Example)

Introduction

Casting in Python is the process of changing a data type from one form to another. In data analysis, this is very important because real-world datasets often contain values in the wrong format (for example, numbers stored as text or floats that should be integers).

In this article, we will use Pandas to perform casting using an Excel dataset.


Installing Required Libraries

Before working with Excel files in Python, you need to install the required libraries:

To check if pandas is installed, run the following command.

PS C:\Users\User> pip install pandas openpyxl
Enter fullscreen mode Exit fullscreen mode

if already installed your terminal will return this:

Requirement already satisfied: pandas in c:\users\user\anaconda3\lib\site-packages (2.3.3)
Requirement already satisfied: openpyxl in c:\users\user\anaconda3\lib\site-packages (3.1.5)
Requirement already satisfied: numpy>=1.26.0 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2.3.5)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2025.2)
Requirement already satisfied: et-xmlfile in c:\users\user\anaconda3\lib\site-packages (from openpyxl) (2.0.0)
Requirement already satisfied: six>=1.5 in c:\users\user\anaconda3\lib\site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0

Enter fullscreen mode Exit fullscreen mode

check your python version or rather start your python program Run this command

PS C:\Users\User> python
Python 3.13.9 | packaged by Anaconda, Inc. | (main, Oct 21 2025, 19:09:58) [MSC v.1929 64 bit (AMD64)] on win32
Enter fullscreen mode Exit fullscreen mode

Why these libraries?

pandas โ†’ Used for data analysis and handling tables
openpyxl โ†’ Allows Python to read/write Excel (.xlsx) files

Loading the Dataset

We start by importing pandas and loading the Excel file.

Step 1: Import pandas

import pandas as pd
Enter fullscreen mode Exit fullscreen mode

Step 2: Load Excel file

>>> pd.read_excel(r"C:\Users\User\OneDrive\Documents\casting_practice.xlsx")
Enter fullscreen mode Exit fullscreen mode

ID Name Age_str Salary_str JoinDate_str Active_str
1 Mary 25 45000.50 2024-01-10 TRUE
2 John 30 60000.00 2023-05-21 FALSE
3 Amina 22 32000.75 2025-03-15 TRUE
4 Brian 28 50000.10 2022-11-01 TRUE
5 Zawadi 35 75000.99 2021-07-19 FALSE
6 Kevin 27 41000.00 2023-09-12 TRUE
7 Grace 24 38000.55 2024-06-30 FALSE
8 Samuel 31 67000.80 2020-02-18 TRUE
9 Linda 29 52000.25 2022-12-05 TRUE
10 Peter 33 71000.00 2021-08-22 FALSE

to Shows the data type of each column in your dataset

run This

df=pd.read_excel(r"C:\Users\User\OneDrive\Documents\casting_practice.xlsx")
>>> df.dtypes
Enter fullscreen mode Exit fullscreen mode

ID int64
Name object
Age_str int64
Salary_str float64
JoinDate_str datetime64[ns]
Active_str bool
dtype: object
# Now you can go ahead and change data types

  • #changing age fro int to float
df["Age_str"] = df["Age_str"] . astype(float)
Enter fullscreen mode Exit fullscreen mode
print(df["Age_str"])
Enter fullscreen mode Exit fullscreen mode

0 25.0
1 30.0
2 22.0
3 28.0
4 35.0
5 27.0
6 24.0
7 31.0
8 29.0
9 33.0
Name: Age_str, dtype: float64

changing int to float

 df["Age_str"] = df["Age_str"] . astype(float)
 print(df["Age_str"])
Enter fullscreen mode Exit fullscreen mode

0 25.0
1 30.0
2 22.0
3 28.0
4 35.0
5 27.0
6 24.0
7 31.0
8 29.0
9 33.0
Name: Age_str, dtype: float64

In Conclusion

Casting is a fundamental skill in Python data analysis. It helps ensure datasets are clean and ready for analysis or machine learning.
I encourage you to practice more on this concept for easy understanding.

Top comments (0)