How To Select Specific Data Type Columns?
Table Of Contents:
- Syntax To Select Specific DataType Columns.
- Examples Of Selecting Specific DataTypes.
(1) Syntax:
DataFrame.select_dtypes(include=None, exclude=None)
Description:
- This method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage.
Parameters:
- include, exclude: scalar or list-like – A selection of dtypes or strings to be included/excluded. At least one of these parameters must be supplied.
Returns:
- DataFrame – The subset of the frame including the dtypes in
include
and excluding the dtypes inexclude
.
Raises:
ValueError–
- If both of
include
andexclude
are empty - If
include
andexclude
have overlapping elements - If any kind of string dtype is passed in.
- If both of
Note:
- To select all numeric types, use
np.number
or'number'
To select strings you must use the
object
dtype, but note that this will return all object dtype columnsSee the numpy dtype hierarchy
To select datetimes, use
np.datetime64
,'datetime'
or'datetime64'
To select timedeltas, use
np.timedelta64
,'timedelta'
or'timedelta64'
To select Pandas categorical dtypes, use
'category'
To select Pandas datetimetz dtypes, use
'datetimetz'
(new in 0.20.0) or'datetime64[ns, tz]'
(2) Examples Of Selecting Specific DataType Columns :
Example-1
import pandas as pd
student = {'Name':['Subrat','Abhispa','Arpita','Anuradha','Namita'],
'Roll_No':[100,101,102,103,104],
'Subject':['Math','English','Science','History','Commerce'],
'Mark':[95,88,76,73,93]}
student_object = pd.DataFrame(student)
student_object
Output:
student_object.select_dtypes(include='number')
Output:
student_object.select_dtypes(include='object')
Output:
student_object.select_dtypes(exclude='object')