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)
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
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']
#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())
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)
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
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']
#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())