Get Type & Class of All Columns in pandas DataFrame in Python (Example Code)

In this article you’ll learn how to check the type of all columns in a pandas DataFrame in Python programming.

Setting up the Example

import pandas as pd                         # Import pandas library
my_df = pd.DataFrame({'A':range(22, 29),    # Constructing a pandas DataFrame
                      'B':['x', 'x', 'z', 'y', 'x', 'y', 'x'],
                      'C':[True, True, True, False, True, False, True],
                      'D':range(208, 201, - 1)})
print(my_df)
#     A  B      C    D
# 0  22  x   True  208
# 1  23  x   True  207
# 2  24  z   True  206
# 3  25  y  False  205
# 4  26  x   True  204
# 5  27  y  False  203
# 6  28  x   True  202

Example: Check All Data Types of Each Column in a pandas DataFrame

print(my_df.dtypes)                         # Print type of all columns
# A     int64
# B    object
# C      bool
# D     int64
# dtype: object

Further Resources & Related Tutorials

Have a look at the following Python programming tutorials. They discuss topics such as descriptive statistics, text elements, data conversion, and naming data:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top