计算不包含一些字符串 Pandas DataFrames 的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17836237/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count rows which do not contain some string-Pandas DataFrames
提问by Nilani Algiriyage
I want to count the rows where the dataframe do not contain some string. Eg:
我想计算数据框不包含某些字符串的行。例如:
df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y','x/y/z','x','x/u/v/w']]).T
df.columns = ['col1','col2','col3']
   col1 col2     col3
0   1.1    A    x/y/z
1   1.1    A      x/y
2   1.1    A  x/y/z/n
3   2.6    B      x/u
4   2.5    B        x
5   3.4    B    x/u/v
6   2.6    B    x/y/z
7   2.6    A        x
8   3.4    B  x/u/v/b
9   3.4    C        -
10  2.6    B      x/y
11  1.1    D    x/y/z
12  1.1    D        x
13  3.3    D  x/u/v/w
In the above dataframe I want to count the rows which do not contain 'u' or 'z'. I know how to use str.contains to get the rows with specific strings.
在上面的数据框中,我想计算不包含“u”或“z”的行。我知道如何使用 str.contains 来获取具有特定字符串的行。
df.col3.str.contains('u|z')
How to get the count of "not" part?
如何获得“非”部分的数量?
回答by waitingkuo
Try:
尝试:
~df.col3.str.contains('u|z')
Update
更新
To Count, use
要计数,请使用
(~df.col3.str.contains('u|z')).sum()
回答by nwalsh
I might be misunderstanding but isn't this possible?
我可能会误解,但这不可能吗?
if not df.col3.str.contains('u|z'):
if not df.col3.str.contains('u|z'):
or
或者
if df.col3.str.contains('u|z'):
    #do something
else: # will not contain a u or a z        
    #do something

