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.
CodePudding user response:
how about this?
>>> (df["column"].str.contains('\?')).astype('int').sum()
CodePudding user response:
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']
