How to check the data type in pandas DataFrame?

How to check the data type in pandas DataFrame?

To check the data type in pandas DataFrame we can use the "dtype" attribute. The attribute returns a series with the data type of each column.

And the column names of the DataFrame are represented as the index of the resultant series object and the corresponding data types are returned as values of the series object.

If any column has mixed data types are stored then the data type of the entire column is indicated as object dtype.

Example 1

Apply the pandas dtype property and verify the data type of each in the DataFrame object.

# importing pandas package
import pandas as pd

# create a Pandas DataFrame
df = pd.DataFrame({'Col1':[4.1, 23.43], 'Col2':['a', 'w'], 'Col3':[1, 8]})

print("DataFrame:")
print(df)

# apply the dtype attribute
result = df.dtypes

print("Output:")
print(result)         

Output

The output is as follows ?

DataFrame:
? ? ? ? ? ? ? A ? ? ? B ? ? ? C
0 ? ? ? ? ? ?41 ? ? ? 1 ? ?1.30
1 23 2021-01-01 ? ?3.23
2 ? ? ? ? ? ?56 ? 34.34 ?267.30

Output:
A ? ? ?int64
B ? ? object
C ? ?float64
dtype: object        

For the given DataFrame column B has stored the mixed data types values so that the resultant dtype of that particular column is represented as an object dtype.

Suhail Sallam

Intrested in Data Science ,Analytics, Strearmlit and Power BI, Looker, Tableau

6 个月

Thanks

赞
回复

要查看或添加评论,请登录

Tutorialspoint的更多文章

社区洞察

其他会员也浏览了