Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count rows which do not contain some string-Pandas DataFrames

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?

like image 736
Nilani Algiriyage Avatar asked Jul 24 '13 14:07

Nilani Algiriyage


1 Answers

Try:

~df.col3.str.contains('u|z')

Update

To Count, use

(~df.col3.str.contains('u|z')).sum()
like image 53
waitingkuo Avatar answered Oct 19 '22 05:10

waitingkuo