+

Python | Pandas DataFrame.dtypes

DataFrame.dtypes Pandas DataFrame.dtypes returns dtypes to the DataFrame. Returns a Series with the data type of each column.

Syntax: DataFrame.dtypes

Parameter: None

Returns: dtype of each column

Example # 1: Use the DataFrame.dtypes attribute to find out the data type (dtype) of each column in a given data frame.

# import pandas as pd

import pandas as pd

 
# Create DataFrame

df = pd.DataFrame ({ `Weight` : [ 45 , 88  , 56 , 15 , 71 ],

  `Name` : [ ` Sam` , `Andrea` , ` Alex` , `Robin` , ` Kia` ],

` Age` : [ 14 , 25 , 55 , 8 , 21 ]} )

 
# Create index

index_ = [ `Row_1 ` , ` Row_2` , `Row_3` , ` Row_4` , `Row_5` ]

  
# Set index

df.index = index_

 
# Print DataFrame

print (df)

Output:

We will now use the DataFrame.dtypes attribute to find out the data type of each column in a given frame data.

# return the dtype of each column

result = df.dtypes

 
# Print result

print (result)

Output:

As we can see in the output, the DataFrame.dtypes attribute has successfully returned the data types of each column in the given dataframe .

Example # 2: AND Use the DataFrame.dtypes attribute to find out the data type (dtype) of each column in a given data frame.

# import pandas as pd

import pandas as pd

  
# Create DataFrame

df = pd.DataFrame ({ "A" : [ 12 , 4 , 5 , None , 1 ], 

" B " : [ 7 , 2 , 54 , 3 , None ], 

"C" : [ 20 , 16 , 11 , 3 , 8 ], 

"D" : [ 14 , 3 , None , < code class = "value"> 2 , 6 ]}) 

 
# Create Index

index_ = [ `Row_1` , `Row_2` , ` Row_3` , `Row_4` , `Row_5` ]

  
# Set index

df.index = index_

 
# Print DataFrame

print (df)

Output:

Now we will use the DataFrame.dtypes attribute to find out the data type of each column in a given data frame.

# return the dtype of each column

result = df.dtypes

 
# Print result

print (result)

Output:

As we can see in the output, the DataFrame.dtypes successful o returned the data types of each column in the given dataframe.

Get Solution for free from DataCamp guru