I want to count the rows where the dataframe do not contain some string. Eg:
我要计算dataframe不包含字符串的行。例如:
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.
在上面的dataframe中,我想计算不包含“u”或“z”的行。我知道如何使用string .contains以获得带有特定字符串的行。
df.col3.str.contains('u|z')
How to get the count of "not" part?
如何得到“不”部分的计数?
2 个解决方案
#1
16
Try:
试一试:
~df.col3.str.contains('u|z')
Update
To Count, use
数,使用
(~df.col3.str.contains('u|z')).sum()
#2
-1
I might be misunderstanding but isn't this possible?
我可能是误会了,但这不可能吗?
if not df.col3.str.contains('u|z'):
如果不是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
#1
16
Try:
试一试:
~df.col3.str.contains('u|z')
Update
To Count, use
数,使用
(~df.col3.str.contains('u|z')).sum()
#2
-1
I might be misunderstanding but isn't this possible?
我可能是误会了,但这不可能吗?
if not df.col3.str.contains('u|z'):
如果不是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