简体   繁体   中英

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.

这个怎么样?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM