Python:根据布尔条件消除 Pandas DataFrame 中的行

Python: Eliminating rows in Pandas DataFrame based on boolean condition

假设我在 Pandas 中有一个 DataFrame,例如

   c1   c2
0  'ab'  1
1  'ac'  0
2  'bd'  0
3  'fa'  1
4  'de'  0

并且我希望它显示所有行,以便 c1 不包含 'a'。我想要的输出是:

   c1   c2
2  'bd'  0
4  'de'  0

我的第一次尝试是使用 df.loc,像这样:

df.loc['a' not in df['c1']]

对于搜索特定值,df.loc 可以正常工作,但对于基于 False 条件('a' 不在 df['c1'] 中的搜索)则不行。

我知道我可以做相反的事情。我的意思是,我可以 return 通过此代码 'c1' 列中包含 'a' 的所有行:

df.loc[df['c1'].str.contains('a')]

但我就是想不出 elegant/concise 反过来的方法。我该怎么做?

使用~翻转你的布尔系列:

df.loc[~df['c1'].str.contains('a')]