且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 pandas 数据框中删除包含特定列中特定字符串的行?

更新时间:2023-12-03 19:52:04

pandas具有向量化的字符串操作,因此您可以过滤掉包含不需要的字符串的行:

pandas has vectorized string operations, so you can just filter out the rows that contain the string you don't want:

In [91]: df = pd.DataFrame(dict(A=[5,3,5,6], C=["foo","bar","fooXYZbar", "bat"]))

In [92]: df
Out[92]:
   A          C
0  5        foo
1  3        bar
2  5  fooXYZbar
3  6        bat

In [93]: df[~df.C.str.contains("XYZ")]
Out[93]:
   A    C
0  5  foo
1  3  bar
3  6  bat