How to Check Data Types in Pandas DataFrame in Python
To check data types of columns in a pandas DataFrame, use
df.dtypes to see each column's type or df.info() for a summary including data types. These commands help you understand what kind of data each column holds.Syntax
df.dtypes returns a Series showing the data type of each column in the DataFrame df.
df.info() prints a summary of the DataFrame including the number of non-null values and data types of each column.
python
df.dtypes df.info()
Example
This example creates a simple pandas DataFrame and shows how to check the data types of its columns using dtypes and info().
python
import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Height': [5.5, 6.0, 5.8], 'Member': [True, False, True]} df = pd.DataFrame(data) # Check data types of each column print(df.dtypes) # Get summary info including data types print('\nDataFrame info:') df.info()
Output
Name object
Age int64
Height float64
Member bool
dtype: object
DataFrame info:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 Height 3 non-null float64
3 Member 3 non-null bool
dtypes: bool(1), float64(1), int64(1), object(1)
memory usage: 311.0 bytes
Common Pitfalls
- Using
type(df)only shows the type of the whole object (DataFrame), not the columns. - Confusing
df.dtypesoutput with Python built-in types; pandas uses its own types likeint64,float64, andobject. - For mixed types in a column, pandas shows
object, which means it can hold any Python object.
python
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': ['x', 'y']}) # Wrong: This shows DataFrame type, not column types print(type(df)) # Output: <class 'pandas.core.frame.DataFrame'> # Right: Shows data types of each column print(df.dtypes)
Output
<class 'pandas.core.frame.DataFrame'>
A int64
B object
dtype: object
Quick Reference
Use these commands to check data types in pandas:
df.dtypes: Returns a Series with data types of each column.df.info(): Prints a summary including data types and non-null counts.df.select_dtypes(include=[type]): Select columns of a specific data type.
| Command | Description |
|---|---|
| df.dtypes | Shows data type of each column |
| df.info() | Prints summary with data types and non-null counts |
| df.select_dtypes(include=[type]) | Selects columns of a specific data type |
Key Takeaways
Use df.dtypes to see the data type of each column in a pandas DataFrame.
Use df.info() for a detailed summary including data types and non-null counts.
Remember df.dtypes returns pandas-specific types like int64, float64, and object.
Avoid using type(df) to check column data types; it only shows the DataFrame type.
Use df.select_dtypes() to filter columns by their data type.