0

Columns L,M,N of my dataframe are populated with 'true' and 'false' statements(1000 rows). I would like to create a new column 'count_false' that will return the number of times 'false' statement occurred in columns L,M and N.

Any tips appreciated!

Thank you.

jpp
  • 159,742
  • 34
  • 281
  • 339
Newby
  • 1
  • 1

3 Answers3

3

You can negate your dataframe and sum over axis=1:

df = pd.DataFrame(np.random.randint(0, 2, (5, 3)), columns=list('LMN')).astype(bool)

df['Falses'] = (~df).sum(1)

print(df)

       L      M      N  Falses
0   True  False   True       1
1   True  False  False       2
2   True   True   True       0
3  False   True  False       2
4  False  False   True       2

If you have additional columns, you can filter accordingly:

df['Falses'] = (~df[list('LMN')]).sum(1)
jpp
  • 159,742
  • 34
  • 281
  • 339
  • This is the simplest. To count trues with a list of relevant column names: df["Trues"] = df[["col1, "col2", "coln"]].sum(1) – DrWhat Sep 13 '21 at 13:32
0

Try this : df[df==false].count()

vhadalgi
  • 7,027
  • 6
  • 38
  • 67
  • 1
    Well that won't work because the Boolean `False` is capitalised. Is the OP working with strings or bools? – roganjosh Aug 08 '18 at 11:38
0

As explained in this Stack question True and False equal to 1 and 0 in Python, therefore something like the line three of the following example should solve your problem:

import pandas as pd
df = pd.DataFrame([[True, False, True],[False, False, False],[True, False, True],[False, False, True]], columns=['L','M','N'])
df['count_false'] = 3 - (df['L']*1 + df['M']*1 + df['N']*1)
FilippoBu
  • 100
  • 5