How To Select Specific Data Type Columns?


How To Select Specific Data Type Columns?

Table Of Contents:

  1. Syntax To Select Specific DataType Columns.
  2. 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:

  1. 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:

  1. DataFrame – The subset of the frame including the dtypes in include and excluding the dtypes in exclude.

Raises:

   ValueError

    • If both of include and exclude are empty
    • If include and exclude have overlapping elements
    • If any kind of string dtype is passed in.

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 columns

  • See 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')

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *