How to describe columns as categorical values?
Solution 1
following converts all columns to object
type then describes them:
df.astype('object').describe()
for cleaner view try:
df.astype('object').describe().transpose()
Solution 2
a slightly shorter version of the answer:
df.describe(include = 'object')
Zahra
by day, playing in my vim playground, creating algorithms, bugging, debugging, and so on. by night, a doulingo points gatherer!
Updated on July 17, 2022Comments
-
Zahra almost 2 years
I have a pandas dataframe that contains a mix of categorical and numeric columns. By default,
df.describe()
returns only a summary of the numerical data (describing those columns withcount
,mean
,std
,min
,quantiles
,max
)when iterating through all the columns in the df and describing them individually as
[df[c].describe() for c in df.columns]
the description is returned based off of specific column dtype; i.e. numerical summary forint
andfloat
and categoric summary forobject
Does any one know of a succinct way of describing all columns as categorical with
count
,unique
,top
,freq
? -
Arthur Khazbs almost 2 yearsThank you so much! By the way, for shorter code, try
T
instead oftranspose()