我有一个看起来像这样的数据帧:
df4 = pd.DataFrame({'Q':['chair', 'desk', '-----monitor', 'chair'], 'R':['red', '-- use blue or dark blue', 'yellow', 'purple'], 'S': ['-- is english spoken?', 'german', 'spanish', 'english']})
Q R S
0 chair Red -- is english spoken?
1 desk -- blue or dark blue german
2 -----monitor yellow spanish
3 chair purple english
我想退回的内容:
Q R S
3 chair purple english
如果任何列的"-"值在开头出现 2 次或更多次,我想过滤掉整行。
我找到了一个用于过滤数值的线程,但是有什么方法可以过滤掉特殊字符吗?特别是正则表达式?
编辑#1:
如果"-"在一开始出现 2 次或更多次,我只想删除行。如果该值出现在某些文本的中间,那很好。
假设我的数据帧如下所示:
Q R S
0 chair Red -- is english spoken?
1 desk blue or dark blue ger--man
2 -----monitor yellow spanish
3 chair purple english
我会返回这个:
Q R S
1 desk blue or dark blue ger--man
3 chair purple english
编辑#2:
我试过这个:
df4[~df4.Q.str.startswith(('--'))]
但这仅适用于 1 列,而不是全部。
将applymap
与in
和any
一起使用
df4[~df4.applymap(lambda x : '--' in x).any(1)]
Out[287]:
Q R S
3 chair purple english
更新仅在开始时排除某些。
df4[~df4.applymap(lambda x : str.startswith(x,'--')).any(1)]