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.
df.col3.str.contains('u|z')
How to get the count of "not" part?
Try:
~df.col3.str.contains('u|z')
To Count, use
(~df.col3.str.contains('u|z')).sum()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With