Pandas: How to Check dtype for All Columns in DataFrame


You can use the following methods to check the data type (dtype) for columns in a pandas DataFrame:

Method 1: Check dtype of One Column

df.column_name.dtype

Method 2: Check dtype of All Columns

df.dtypes

Method 3: Check which Columns have Specific dtype

df.dtypes[df.dtypes == 'int64']

Method 4: Use select_dtypes() to Filter Columns

df.select_dtypes(include=['int64'])

Method 5: Get Detailed Information with info() Method

df.info()

The following examples show how to use each method with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'points': [18, 22, 19, 14, 14, 11],
                   'assists': [5, 7, 7, 9, 12, 9],
                   'all_star': [True, False, False, True, True, True]})

#view DataFrame
print(df)

  team  points  assists  all_star
0    A      18        5      True
1    B      22        7     False
2    C      19        7     False
3    D      14        9      True
4    E      14       12      True
5    F      11        9      True

Example 1: Check dtype of One Column

We can use the following syntax to check the data type of just the points column in the DataFrame:

#check dtype of points column
df.points.dtype

dtype('int64')

From the output we can see that the points column has a data type of integer.

Example 2: Check dtype of All Columns

We can use the following syntax to check the data type of all columns in the DataFrame:

#check dtype of all columns
df.dtypes

team        object
points       int64
assists      int64
all_star      bool
dtype: object

From the output we can see:

  • team column: object (this is the same as a string)
  • points column: integer
  • assists column: integer
  • all_star column: boolean

By using this one line of code, we can see the data type of each column in the DataFrame.

Example 3: Check which Columns have Specific dtype

We can use the following syntax to check which columns in the DataFrame have a data type of int64:

#show all columns that have a class of int64
df.dtypes[df.dtypes == 'int64']

points     int64
assists    int64
dtype: object

From the output we can see that the points and assists columns both have a data type of int64.

We can use similar syntax to check which columns have other data types.

For example, we can use the following syntax to check which columns in the DataFrame have a data type of object:

#show all columns that have a class of object (i.e. string)
df.dtypes[df.dtypes == 'O']

team    object
dtype: object

We can see that only the team column has a data type of ‘O’, which stands for object.

Example 4: Use select_dtypes() to Filter Columns

The select_dtypes() method provides a more flexible way to select columns based on their data types. This method returns a subset of the DataFrame’s columns based on the requested dtypes:

#select only integer columns
df.select_dtypes(include=['int64'])

   points  assists
0      18        5
1      22        7
2      19        7
3      14        9
4      14       12
5      11        9

This returns a DataFrame containing only the columns that have the int64 data type.

We can also exclude specific data types:

#select all columns except integers
df.select_dtypes(exclude=['int64'])

  team  all_star
0    A      True
1    B     False
2    C     False
3    D      True
4    E      True
5    F      True

This returns all columns that don’t have the int64 data type.

You can also select multiple data types at once:

#select both object and boolean columns
df.select_dtypes(include=['object', 'bool'])

  team  all_star
0    A      True
1    B     False
2    C     False
3    D      True
4    E      True
5    F      True

Example 5: Get Detailed Information with info() Method

The info() method provides a concise summary of the DataFrame, including the data types and non-null values count:

#get detailed DataFrame information
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   team      6 non-null      object
 1   points    6 non-null      int64 
 2   assists   6 non-null      int64 
 3   all_star  6 non-null      bool  
dtypes: bool(1), int64(2), object(1)
memory usage: 320.0+ bytes

The info() method provides additional useful information beyond just data types:

  • Index range
  • Column names
  • Non-null value counts for each column
  • Data types for each column
  • Summary of data types
  • Memory usage

This method is especially useful when working with larger datasets to quickly assess the structure and data types.

Wrapping Up

Checking and understanding the data types in your DataFrame is an essential part of data analysis with pandas. The methods covered in this tutorial offer different approaches depending on your specific needs:

  • Use df.column_name.dtype when you need to check a single column’s data type
  • Use df.dtypes to get a complete overview of all column types in one go
  • Use filtering with df.dtypes[condition] to identify columns with specific data types
  • Use df.select_dtypes() when you need to work with a subset of columns based on their types
  • Use df.info() for a comprehensive overview of your DataFrame’s structure and types

Understanding data types helps prevent errors during analysis and ensures operations are performed correctly. It’s often one of the first steps when exploring new datasets, as it informs how you’ll need to process and analyze the data.

Additional Resources

The following tutorials explain how to perform other common operations on pandas DataFrames:

Pandas: How to Get Cell Value from DataFrame
Pandas: Get Index of Rows Whose Column Matches Value
Pandas: How to Set Column as Index

Leave a Reply

Your email address will not be published. Required fields are marked *