Skip to content
Advertisement

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.

Advertisement

Answer

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']

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement