How to get the data type of each column of a Pandas DataFrame using Python
• 1 minKnowing what types of data you are working with can be crucial.
Sometimes a number or a date can be understood as a string by Pandas and for that reason you might end up with a Python error when summing columns.
This is why it is important to check that the data type of all columns are the right one.
To do so, we can use the DataFrame.dtypes attributes which will give you the types of each column in a Pandas DataFrame
# How to get data types of columns in a Pandas Dataframe
# Import the Pandas library
import pandas as pd
# We create our example dataframe
df = pd.DataFrame({"product" : ["Stickers", "T-shirts", "Mug", "Stickers", "Jeans", "Mug"],
"sales_in_usd" : [10000, 2142, 3321, 11141, 12133, 3321],
"year" : [2020, 2020, 2020, 2021, 2021, 2021]})
# We print the data types of columns in a Pandas DataFrame
print(df.dtypes)
Here you are! You now know how to get the data type of each column of a Pandas DataFrame using Python.
More on DataFrames
If you want to know more about DataFrame and Pandas. Check out the other articles I wrote on the topic, just here :