How to check the data type in pandas DataFrame?
Tutorialspoint
Online free tutorials library offering crisp and easy learning on any IT & software topic and other allied subjects!
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.
Intrested in Data Science ,Analytics, Strearmlit and Power BI, Looker, Tableau
6 个月Thanks