
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check 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 mentioned below ?
DataFrame: Col1 Col2 Col3 0 4.10 a 1 1 23.43 w 8 Output: Col1 float64 Col2 object Col3 int64 dtype: object
In this output block, we can notice that Col1 has float64 type data, Col2 has object data and column Col3 has stored the integer (int64) type data.
Example 2
Now, let us apply the dtype attribute to another Pandas DataFrame object.
# importing pandas package import pandas as pd # create a Pandas DataFrame df = pd.DataFrame({'A':[41, 23, 56], 'B':[1, '2021-01-01', 34.34], 'C':[1.3, 3.23, 267.3]}) 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.