我有一個dtype 'object'包含數值和 value的 pandas 列'?'。
我應該如何繼續計算具有該值的行數'?'?
我正在嘗試運行:
question_mark_count = df['column'].str.contains('\?').sum()
在具有數值和一些問號“?”的列中,但我收到錯誤訊息:
AttributeError: Can only use .str accessor with string values!
當我運行時df.dtypes,我可以看到該列是'object' type.
我還嘗試將列轉換為字串:
df["column"] = df["column"].astype("string")
但我仍然遇到同樣的錯誤。
uj5u.com熱心網友回復:
這個怎么樣?
>>> (df["column"].str.contains('\?')).astype('int').sum()
uj5u.com熱心網友回復:
進一步探索可能性:
df["column"].str.contains('\?').value_counts()
免疫np.nan pd.NA ints floats或任何你在你的df['column']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333445.html
上一篇:有效地計算日期在兩列之間的記錄
