jimmy
jimmy

Reputation: 515

How to count number of rows with a specific string value in a column using pandas?

I have a pandas column with dtype 'object' that contains numeric values and the value '?'.

How should I proceed to count the number of rows that have the value '?' ?

I'm trying to run:

question_mark_count = df['column'].str.contains('\?').sum()

in a column that has numeric value and some question marks '?', but I'm getting the error:

AttributeError: Can only use .str accessor with string values!

When I run df.dtypes, I can see that the column is 'object' type.

I've also tried to convert the column to string:

df["column"] = df["column"].astype("string")

But I'm still getting the same error.

Upvotes: 1

Views: 5044

Answers (3)

Morita Ichika
Morita Ichika

Reputation: 21

In my case the previous answer is almost correct. Try to add na=False in the call to the contains function:

df["column"].str.contains('\?', na=False).astype('int').sum()

Upvotes: 0

eliu
eliu

Reputation: 2479

to further explore possibilities:

df["column"].str.contains('\?').value_counts()

immune to np.nan pd.NA ints floats or whatever you have in your df['column']

Upvotes: 1

Danail Petrov
Danail Petrov

Reputation: 1875

how about this?

>>> (df["column"].str.contains('\?')).astype('int').sum()

Upvotes: 1

Related Questions