DataFrame.select_dtypes()

Pandas

select_dtypes(self, include=None, exclude=None)
select_dtypes() : to get subset of DataFrame of particular dtypes.

Parametersinclude or exclude the selection dtypes
ReturnsDataFrame to include ( or exclude ) dtypes
RaiseValueError , if Parameters is not given

Let us create a DataFrame having all data types. From this DataFrame we will collect all int dtypes.
import pandas as pd 
td = pd.Series([pd.Timedelta(days=i) for i in range(6)])  
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'Avg_mark':[45.5,48.09,50.12,55.1,50.6,55.6],
         'dt_start':['1/1/2020','2/1/2020','5/1/2020','11/7/2020',
			'15/8/2020','31/12/2020'],
         'Exam':[True,False,True,True,False,False],
         'dt':td,
         'grade':['a', 'c', 'b', 'b','b','c']}

my_data = pd.DataFrame(data=my_dict)
my_data['grade']=my_data['grade'].astype('category')
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'])
print(my_data.select_dtypes(include='int64'))
Output
   ID  MATH
0   1    80
1   2    40
2   3    70
3   4    70
4   5    70
5   6    30
Using more than one dtypes using a list.
print(my_data.select_dtypes(include=['int64','bool']))
Output
   ID  MATH   Exam
0   1    80   True
1   2    40  False
2   3    70   True
3   4    70   True
4   5    70  False
5   6    30  False

Using exclude

We will remove two dtypes int64 and float64, other dtypes will be returned.
print(my_data.select_dtypes(exclude=['int64','float64']))
Output
   NAME   dt_start   Exam     dt grade
0  Ravi 2020-01-01   True 0 days     a
1  Raju 2020-02-01  False 1 days     c
2  Alex 2020-05-01   True 2 days     b
3   Ron 2020-11-07   True 3 days     b
4  King 2020-08-15  False 4 days     b
5  Jack 2020-12-31  False 5 days     c
Pandas to_timedelta() astype() select_dtypes() timedelta64()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer