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

Question:

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)
Asked By: Dhruv

||

Answers:

data.info() or data.dtypes will give you overall statistics of columns.

If you want the datatype of each column in one command do dict(data.dtypes)


for x in data.columns:
    if data[x].dtype == object:
        #do something
Answered By: God Is One

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
Answered By: mozway

you can use .dtypes

here’s the example:

import pandas as pd
df = pd.DataFrame({'A':[5,2,3], 'B':['a','b','c']})
>>> df.dtypes

A     int64
B    object
dtype: object

and if you want to find all object column names

>>> [k for k,v in dict(df.dtypes).items() if v == object]

['B']
Answered By: yulaf

Use DataFrame.select_dtypes:

#get all columns by dtypes == object
print (df.select_dtypes('object'))

#processing only object columns - here call describe
agg = df.select_dtypes('object').describe()

#default describe omit non numeric columns
describe = df.describe()

#joined together
out = pd.concat([describe,agg], axis=1)

Or:

out = df.apply(lambda x: x.describe())
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.