0
0
Pandasdata~5 mins

Using appropriate dtypes in Pandas

Choose your learning style9 modes available
Introduction

Using the right data types helps your computer work faster and use less memory when handling data.

When you have a large table and want to save memory.
When you want to speed up calculations on your data.
When you want to make sure numbers are treated as numbers, not text.
When you want to prepare data for machine learning models.
When you want to avoid errors caused by wrong data types.
Syntax
Pandas
df = df.astype({'column_name': 'dtype'})

You can change one or many columns at once by passing a dictionary.

Common dtypes include 'int', 'float', 'category', and 'bool'.

Examples
Change the 'age' column to integer type.
Pandas
df['age'] = df['age'].astype('int')
Change 'gender' to category and 'income' to float in one step.
Pandas
df = df.astype({'gender': 'category', 'income': 'float'})
Convert 'is_student' column to boolean type.
Pandas
df['is_student'] = df['is_student'].astype('bool')
Sample Program

This code creates a table with all columns as text. Then it changes each column to a better type: numbers to int or float, categories to category type, and true/false to boolean.

Pandas
import pandas as pd

# Create a sample DataFrame
 data = {'age': ['25', '30', '22', '40'],
         'gender': ['M', 'F', 'F', 'M'],
         'income': ['50000', '60000', '45000', '80000'],
         'is_student': ['False', 'True', 'False', 'False']}

df = pd.DataFrame(data)

print('Before changing dtypes:')
print(df.dtypes)

# Change dtypes to appropriate types
df = df.astype({'age': 'int', 'gender': 'category', 'income': 'float', 'is_student': 'bool'})

print('\nAfter changing dtypes:')
print(df.dtypes)
OutputSuccess
Important Notes

Using 'category' for columns with few unique values saves memory and speeds up operations.

Be careful: converting to wrong dtype can cause errors or data loss.

Use df.info() to check memory usage before and after changing dtypes.

Summary

Choosing the right data type makes your data smaller and faster to work with.

You can change data types easily with the astype() method.

Check your data types often to keep your data clean and efficient.