How to describe columns as categorical values?
Asked Answered
C

3

13

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 with count, 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 for int and float and categoric summary for object

Does any one know of a succinct way of describing all columns as categorical with count, unique, top, freq?

Couperin answered 26/2, 2018 at 21:46 Comment(2)
I think I answered myself while clarifying the question. just tried df.astype('object').describe() and it works!!Couperin
moving my comment to an answer for future reference!Couperin
C
13

following converts all columns to object type then describes them:

df.astype('object').describe()

for cleaner view try:

df.astype('object').describe().transpose()
Couperin answered 26/2, 2018 at 21:54 Comment(1)
Thank you so much! By the way, for shorter code, try T instead of transpose()Neves
T
15

a slightly shorter version of the answer:

df.describe(include = 'object')
Tillis answered 4/12, 2018 at 12:40 Comment(0)
C
13

following converts all columns to object type then describes them:

df.astype('object').describe()

for cleaner view try:

df.astype('object').describe().transpose()
Couperin answered 26/2, 2018 at 21:54 Comment(1)
Thank you so much! By the way, for shorter code, try T instead of transpose()Neves
A
0

Additionally, if YOU want to see descriptions of all columns together(including categorical). you can use:

df.describe(include='all')
Antiquary answered 10/12, 2022 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.