count the number of elements of each type in a pandas column

Viewed 2078

I am new to pandas.

I have a column with the object dtype that contains floats and strings.

I would like to count the number of each in the column.

I figured out a way to do it:

len([v for v in list(df["ColumnA"]) if type(v)==float])
len([v for v in list(df["ColumnA"]) if type(v)==str])

But is there a more direct way to do this?

Ideally, I'd prefer to be able to do something like:

df["ColumnA"].dtypes

and have the output be:

float: 103,544
str:     2,767
1 Answers

This worked:

df["ColumnA"].apply(type).value_counts()

<class 'str'>        2767
<class 'float'>    103544
Name: ColumnA, dtype: int64
Related