
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Select columns with specific datatypes
To select columns with specific datatypes, use the select_dtypes() method and the include parameter. At first, create a DataFrame with 2 columns −
dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6] } )
Now, select the 2 columns with their respective specific datatype −
column1 = dataFrame.select_dtypes(include=['object']).columns column2 = dataFrame.select_dtypes(include=['int64']).columns
Example
Following is the code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6] } ) print"DataFrame ...\n",dataFrame print"\nInfo of the entire dataframe:\n" # get the description print(dataFrame.info()) # select columns with specific datatype column1 = dataFrame.select_dtypes(include=['object']).columns column2 = dataFrame.select_dtypes(include=['int64']).columns print"Column 1 with object type = ",column1 print"Column 2 with int64 type = ",column2
Output
This will produce the following output −
DataFrame ... Roll Number Student 0 5 Jack 1 10 Robin 2 3 Ted 3 8 Marc 4 2 Scarlett 5 9 Kat 6 6 John Info of the entire dataframe: <class 'pandas.core.frame.DataFrame'> RangeIndex: 7 entries, 0 to 6 Data columns (total 2 columns): Roll Number 7 non-null int64 Student 7 non-null object dtypes: int64(1), object(1) memory usage: 184.0+ bytes None Column 1 with object type = Index([u'Student'], dtype='object') Column 2 with int64 type = Index([u'Roll Number'], dtype='object')
- Related Articles
- Python – Get the datatypes of columns
- How to select multiple DataFrame columns using regexp and datatypes
- How to select specific columns in MongoDB query?
- Python Container Datatypes
- MySQL query to select all the records only from a specific column of a table with multiple columns
- Python - Select multiple columns from a Pandas dataframe
- Select specific rows in a range with MySQL?
- Extract csv file specific columns to list in Python
- Python Pandas - Form the Union of two Index objects with different datatypes
- Python Pandas - Select a subset of rows and columns combined
- MySQL query to select a specific string with special characters
- MySQL alias for SELECT * columns?
- MySQL SELECT query to return records with specific month and year
- How to select “last child” with a specific class using CSS?
- Select multiple sums with MySQL query and display them in separate columns?

Advertisements