How to Get the Data Type of a DataFrame Column in Pandas
How can we get the data type of a column in a Pandas DataFrame?
Suppose we have a DataFrame df with columns col1 and col2.
Get all column types
Using df.dtypes
We can use df.dtypes to return the type of each column in our DataFrame.
df.dtypes
This should yield a result like the following:
col1 int64
col2 object
dtype: object
Using df.info()
We could also use df.info() to obtain a detailed summary of our DataFrame, which will include our column data types.
df.info()
This would be our output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 col1 5 non-null int64
1 col2 5 non-null object
dtypes: int64(1), object(1)
memory usage: 148.0+ bytes
Get a single column type using df.dtypes[]
We can use df.dtypes[col_name] to return the type of a single column in our DataFrame.
df.dtypes['col1']
This should yield a result like the following:
dtype('int64')