Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find if all the columns in a dataframe are object dtype?

Let’s say I have a DataFrame as data, and I want to find if every single column in the data frame is an object and use it as an if condition.

example:

describe = data.describe
(if condition to find all the columns are 'object'):
  agg = data.agg(['a','b','c'])
  if not agg.empty:
    describe = pd.concat(describe,agg)
    describe = describe.round(2)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your question is not without ambiguity, but you probably want to combinedtypes, eq and all:

if df.dtypes.eq(object).all():
    # do something

Another option:

if df.select_dtypes(exclude='object').empty:
     # do something
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading