Pandas DataFrame – Get Datatypes of Columns
To get the datatypes of columns in a Pandas DataFrame, call DataFrame.dtypes property. The DataFrame.dtypes property returns an object of type pandas.Series with the datatypes of each column in it.
The syntax to use dtypes property of a DataFrame is
DataFrame.dtypes
In the following program, we have created a DataFrame with specific data and column names. Let us get the datatypes of columns in this DataFrame using DataFrame.dtypes.
Python Program
import pandas as pd
df = pd.DataFrame(
[['abc', 22],
['xyz', 25],
['pqr', 31]],
columns=['name', 'age'])
datatypes = df.dtypes
print(datatypes)
Run Output
name object
age int64
dtype: object
We can print the elements of the returned value by DataFrame.dtypes using a for loop as shown in the following.
Python Program
import pandas as pd
df = pd.DataFrame(
[['abc', 22],
['xyz', 25],
['pqr', 31]],
columns=['name', 'age'])
datatypes = df.dtypes
for dtype in datatypes:
print(dtype)
Run Output
object
int64
Summary
In this tutorial of Python Examples, we learned how to get the datatypes of column in a DataFrame using DataFrame.dtypes property.
Related Tutorials
- How to Delete Column from Pandas DataFrame?
- How to Add Column to Pandas DataFrame?
- How to Get Column Names of Pandas DataFrame?
- How to change Order of Columns in Pandas DataFrame?
- How to Get Columns of Numeric Datatype from DataFrame?
- How to Sort DataFrame by Column in Pandas?
- Pandas DataFrame – Select Column
- How to Change Datatype of Columns in Pandas DataFrame?
- How to Delete Column(s) of Pandas DataFrame?
- How to Change Column Labels in Pandas DataFrame?