pandas 删除列中包含特定字符串的行

piv4azn7  于 2023-09-29  发布在  其他
关注(0)|答案(1)|浏览(113)

我有一个大的pandas数据集,格式如下

col1
11111112322
15211114821
25482136522
45225625656
11125648121

我想删除所有包含1111的行(四个连续的行)以获得以下结果

25482136522
45225625656
11125648121

我试过了,但没有工作:

data = df[df["col1"].str.contains("1111")==False]
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    data1_1 = section1[section1["col1"].str.contains("111111")==False]
  File "C:\Users\henry\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py", line 5575, in __getattr__
    return object.__getattribute__(self, name)
  File "C:\Users\henry\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\accessor.py", line 182, in __get__
    accessor_obj = self._accessor(obj)
  File "C:\Users\henry\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\strings\accessor.py", line 177, in __init__
    self._inferred_dtype = self._validate(data)
  File "C:\Users\henry\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\strings\accessor.py", line 231, in _validate
    raise AttributeError("Can only use .str accessor with string values!")
AttributeError: Can only use .str accessor with string values!. Did you mean: 'std'?
eyh26e7m

eyh26e7m1#

问题是,正如错误代码所述,该列不是字符串列:
属性错误:只能对字符串值使用.str访问器!。你的意思是:std?
所以要对它执行字符串操作,你必须首先将列转换为字符串,然后你的代码才能工作:

df[df["col1"].astype(str).str.contains("1111")==False]

输出量:

col1
2  25482136522
3  45225625656
4  11125648121

相关问题