pythonpandas

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)

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