Yungpythonnoob
Yungpythonnoob

Reputation: 125

How to count trues/falses from a list?

df = [bigdataframe[['Action', 'Adventure','Animation',
                           'Childrens', 'Comedy', 'Crime','Documentary',
                           'Drama', 'Fantasy', 'FilmNoir', 'Horror', 
                            'Musical', 
                           'Mystery', 'Romance','SciFi', 'Thriller', 'War', 
                           'Western']].sum(axis=1) > 1]
df
Out[8]: 
[0         True
 1         True
 2         True
 3         True
 4         True
 5        False
 6         True
 7         True
 8        False
 9         True
 10       False
 11        True
 12        True
 13        True
 14        True
 15       False
 16        True
 17       False
 18        True
 19       False
 20       False
 21        True
 22        True
 23        True
 24       False
 25        True
 26        True
 27        True
 28        True
 29        True

 99970     True
 99971     True
 99972    False
 99973     True
 99974     True
 99975     True
 99976     True
 99977     True
 99978    False
 99979    False
 99980     True
 99981    False
 99982     True
 99983    False
 99984     True
 99985     True
 99986     True
 99987     True
 99988    False
 99989     True
 99990     True
 99991     True
 99992    False
 99993     True
 99994     True
 99995     True
 99996     True
 99997     True
 99998     True
 99999    False
 Length: 100000, dtype: bool]

I have tried:

They are in a list so shouldn't I just be able to count them? Or do I need to assign them numerical values, 1 for true and 0 for false and then use the count or sum function to find how many are true?

Upvotes: 1

Views: 119

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

Demo:

In [386]: df = pd.DataFrame(np.random.rand(5,3), columns=list('ABC'))

In [387]: df
Out[387]:
          A         B         C
0  0.228687  0.647431  0.526471
1  0.795122  0.915011  0.950481
2  0.386244  0.705412  0.420596
3  0.343213  0.928993  0.192527
4  0.201023  0.209281  0.304799

In [388]: df[['A','B','C']].sum(axis=1).gt(1.5)
Out[388]:
0    False
1     True
2     True
3    False
4    False
dtype: bool

In [389]: df[['A','B','C']].sum(axis=1).gt(1.5).sum()
Out[389]: 2

Upvotes: 2

to count number of true in a list

sum(unlist(your.list.object))

Upvotes: 0

Related Questions