看起来很简单。该列通常包含数字,但由于某种原因,其中一些具有非数字字符。我想找到所有这些。我正在使用以下代码:
df_other_values.total_count.str.contains('[^0-9]')
但我收到以下错误:
AttributeError: Can only use .str accessor with string values, which use
np.object_ dtype in pandas
所以我试了这个:
df_other_values = df_other.total_countvalues
df_other_values.total_count.str.contains('[^0-9]')
但收到以下错误:
AttributeError: 'DataFrame' object has no attribute 'total_countvalues'
因此,与其进一步深入兔子洞,我认为一定有一种方法可以做到这一点,而不必将数据帧更改为 np.object。请指教。
谢谢。
我相信
你需要先按astype
投射到string
s,然后按boolean indexing
过滤:
df1 = df[df_other_values.total_count.astype(str).str.contains('[^0-9]')]
带isnumeric
的替代解决方案:
df1 = df[~df_other_values.total_count.astype(str).str.isnumeric()]