Check Column Data Types in a Pandas DataFrame

Kontext Kontext 0 254 0.25 index 9/4/2022

Code description

This code snippet provide examples of checking column data types in a DataFrame using dtype and dtypes.

Sample output:

print(df.dtypes)
a     int64
b    object
dtype: object

print(df.a.dtype)
int64

Code snippet

    import pandas as pd
    
    data = {}
    data['A'] = [1,2,3,4,5]
    data['B'] = ['A','B','C','D','E']
    df = pd.DataFrame(data)
    print(df)
    
    # Check all column types
    print(df.dtypes)
    
    # Check data type of a certain column
    print(df.a.dtype)
pandas python

Join the Discussion

View or add your thoughts below

Comments