The Python Oracle

How to drop DataFrame columns based on dtype

This video explains
How to drop DataFrame columns based on dtype

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
01:11 Accepted answer (Score 56)
01:29 Thank you

--

Full question
https://stackoverflow.com/questions/4881...

Accepted answer links:
[select_dtypes]: https://pandas.pydata.org/pandas-docs/st...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 61


You can use select_dtypes to exclude columns of a particular type.

import pandas as pd

df = pd.DataFrame({'x': ['a', 'b', 'c'], 'y': [1, 2, 3], 'z': ['d', 'e', 'f']})

df = df.select_dtypes(exclude=['object'])
print(df)