Pandas DataFrame.dtypes
Pandas DataFrame.dtypes
The DataFrame.dtypes property in pandas is used to inspect the data types of all columns in a DataFrame. This property is particularly useful for understanding the structure of a dataset and ensuring that the data types are correct for operations.
Syntax
The syntax to access the data types of columns in a DataFrame is:
DataFrame.dtypesHere, DataFrame refers to the pandas DataFrame whose column data types are being inspected.
Examples
Accessing Data Types
To inspect the data types of all columns, use the DataFrame.dtypes property.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Krishna'],
'Age': [25, 32, 40],
'Salary': [50000.5, 60000.0, 70000.0],
'JoiningDate': pd.to_datetime(['2022-01-01', '2021-05-12', '2020-08-15'])
}
df = pd.DataFrame(data)
# Display the data types of all columns
print("Data Types of Columns:")
print(df.dtypes)Output
Data Types of Columns:
Name object
Age int64
Salary float64
JoiningDate datetime64[ns]
dtype: objectChecking the Data Type of a Specific Column
You can check the data type of a specific column using the dtypes property.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Krishna'],
'Age': [25, 32, 40],
'Salary': [50000.5, 60000.0, 70000.0],
'JoiningDate': pd.to_datetime(['2022-01-01', '2021-05-12', '2020-08-15'])
}
df = pd.DataFrame(data)
# Check the data type of a specific column
print("Data type of 'Name':", df.dtypes['Name'])
print("Data type of 'Salary':", df.dtypes['Salary'])Output
Data type of 'Name': object
Data type of 'Salary': float64Filtering Columns by Data Type
Use the dtypes property to filter columns based on their data type.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Krishna'],
'Age': [25, 32, 40],
'Salary': [50000.5, 60000.0, 70000.0],
'JoiningDate': pd.to_datetime(['2022-01-01', '2021-05-12', '2020-08-15'])
}
df = pd.DataFrame(data)
# Filter numeric columns
numeric_columns = df.dtypes[df.dtypes == 'float64'].index
print("Numeric Columns:")
print(df[numeric_columns])Output
Numeric Columns:
Salary
0 50000.5
1 60000.0
2 70000.0Iterating Over Column Data Types
Iterate through the columns and their data types using the dtypes property.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Krishna'],
'Age': [25, 32, 40],
'Salary': [50000.5, 60000.0, 70000.0],
'JoiningDate': pd.to_datetime(['2022-01-01', '2021-05-12', '2020-08-15'])
}
df = pd.DataFrame(data)
# Iterate over columns and their data types
print("Columns and Their Data Types:")
for column, dtype in df.dtypes.items():
print(f"Column: {column}, Data Type: {dtype}")Output
Columns and Their Data Types:
Column: Name, Data Type: object
Column: Age, Data Type: int64
Column: Salary, Data Type: float64
Column: JoiningDate, Data Type: datetime64[ns]Summary
In this tutorial, we explored the DataFrame.dtypes property in pandas. We covered:
- Accessing data types for all columns
- Checking data types of specific columns
- Filtering columns by data type
- Iterating over columns and their data types
Understanding column data types is essential for efficient and error-free data analysis in pandas.