搜索 pandas 列并返回包含任何(一个或多个)非数字字符的所有元素(行)



看起来很简单。该列通常包含数字,但由于某种原因,其中一些具有非数字字符。我想找到所有这些。我正在使用以下代码:

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()]

最新更新