0
0
Pandasdata~5 mins

dtypes for column data types in Pandas

Choose your learning style9 modes available
Introduction

We use dtypes to know what kind of data is stored in each column of a table. This helps us understand and work with the data correctly.

When you want to check if a column has numbers or words.
When you need to convert data from one type to another, like text to numbers.
When you want to find columns with dates to do time calculations.
When you want to make sure data is in the right format before analysis.
When you want to optimize memory by choosing the best data type.
Syntax
Pandas
DataFrame.dtypes

This returns a list showing the data type of each column.

It does not change the data, just shows the types.

Examples
Shows data types of columns 'age' and 'name'.
Pandas
import pandas as pd

data = {'age': [25, 30, 22], 'name': ['Ann', 'Bob', 'Cara']}
df = pd.DataFrame(data)
print(df.dtypes)
Changes 'age' column to float type and shows updated types.
Pandas
df['age'] = df['age'].astype('float')
print(df.dtypes)
Sample Program

This code creates a table with numbers, decimals, true/false, and text. Then it prints the type of data in each column.

Pandas
import pandas as pd

# Create a simple table with different types of data
data = {
    'id': [1, 2, 3],
    'score': [88.5, 92.3, 79.0],
    'passed': [True, True, False],
    'name': ['Alice', 'Bob', 'Charlie']
}
df = pd.DataFrame(data)

# Print the data types of each column
print(df.dtypes)
OutputSuccess
Important Notes

Object type usually means text or mixed data.

Use astype() to change a column's data type.

Knowing dtypes helps avoid errors in calculations and analysis.

Summary

dtypes tell you the kind of data in each column.

They help you understand and prepare data for analysis.

You can check dtypes anytime with DataFrame.dtypes.