Get the data type of column in Pandas - Python - GeeksforGeeks

28 Jul, 2020
 Let’s see how to get data types of columns in the pandas dataframe. First, Let’s create a pandas dataframe.
Example:
Python3
# importing pandas libraryimport pandas as pd  # List of Tuplesemployees = [            ('Stuti', 28, 'Varanasi', 20000),            ('Saumya', 32, 'Delhi', 25000),            ('Aaditya', 25, 'Mumbai', 40000),            ('Saumya', 32, 'Delhi', 35000),            ('Saumya', 32, 'Delhi', 30000),            ('Saumya', 32, 'Mumbai', 20000),            ('Aaditya', 40, 'Dehradun', 24000),            ('Seema', 32, 'Delhi', 70000)            ]       # Create a DataFramedf = pd.DataFrame(employees,                  columns = ['Name', 'Age',                             'City', 'Salary'])# show the dataframedf
Output: 
Dataframe
Method 1: Using Dataframe.dtypes attribute.
This attribute returns a Series with the data type of each column.
Syntax: DataFrame.dtypes.
Parameter: None.
Returns: dtype of each column.
Example 1: Get data types of all columns of a Dataframe.
Python3
# importing pandas libraryimport pandas as pd  # List of Tuplesemployees = [            ('Stuti', 28, 'Varanasi', 20000),            ('Saumya', 32, 'Delhi', 25000),            ('Aaditya', 25, 'Mumbai', 40000),            ('Saumya', 32, 'Delhi', 35000),            ('Saumya', 32, 'Delhi', 30000),            ('Saumya', 32, 'Mumbai', 20000),            ('Aaditya', 40, 'Dehradun', 24000),            ('Seema', 32, 'Delhi', 70000)            ]       # Create a DataFramedf = pd.DataFrame(employees,                  columns = ['Name', 'Age',                             'City', 'Salary'])       # Use Dataframe.dtypes to# give the series of # data types as resultdatatypes = df.dtypes  # Print the data types# of each columndatatypes
Output:
Data types of dataframe
Example 2: Get the data type of single column in a Dataframe.
Python3
#importing pandas libraryimport pandas as pd  # List of Tuplesemployees = [('Stuti', 28, 'Varanasi', 20000),            ('Saumya', 32, 'Delhi', 25000),            ('Aaditya', 25, 'Mumbai', 40000),            ('Saumya', 32, 'Delhi', 35000),            ('Saumya', 32, 'Delhi', 30000),            ('Saumya', 32, 'Mumbai', 20000),            ('Aaditya', 40, 'Dehradun', 24000),            ('Seema', 32, 'Delhi', 70000)            ]  # Create a DataFramedf = pd.DataFrame(employees,                   columns = ['Name', 'Age',                             'City', 'Salary'])  # Use Dataframe.dtypes to give # data type of 'Salary' as resultdatatypes = df.dtypes['Salary']  # Print the data types# of single columndatatypes
Output:
data type of a single column
Method 2: Using Dataframe.info() method.
This method is used to get a concise summary of the dataframe like:
Name of columns
Data type of columns
Rows in Dataframe
non-null entries in each column
It will also print column count, names and data types.
Syntax: DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None)
Return: None and prints a summary of a DataFrame.
Example: Get data types of all columns of a Dataframe.
Python3
# importing pandas libraryimport pandas as pd   # List of Tuplesemployees = [('Stuti', 28, 'Varanasi', 20000),            ('Saumya', 32, 'Delhi', 25000),            ('Aaditya', 25, 'Mumbai', 40000),            ('Saumya', 32, 'Delhi', 35000),            ('Saumya', 32, 'Delhi', 30000),            ('Saumya', 32, 'Mumbai', 20000),            ('Aaditya', 40, 'Dehradun', 24000),            ('Seema', 32, 'Delhi', 70000)            ]  # Create a DataFramedf = pd.DataFrame(employees,                  columns = ['Name', 'Age',                              'City', 'Salary'])  # Print complete details # about the data framedf.info()
Output:
summary of the dataframe including datatypes
Python pandas-datatypes
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Convert integer to string in Python