Sunday 1 October 2023

How to Get the Data Type of Columns in a Pandas DataFrame

‘dtypes’ attribute is used to get the data types of each column in the DataFrame.

 

Example

column_types = df.dtypes

 

dataframe_columns_type.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)

# Get the data types of columns using dtypes attribute
column_types = df.dtypes

print(column_types)

Output

Name    object
Age      int64
City    object
dtype: object


Previous                                                 Next                                                 Home

No comments:

Post a Comment