Home > Blockchain >  how to count and change value in dataframe by row
how to count and change value in dataframe by row

Time:02-16

I want to count the number of times a value False appears in my dataframe and get the number of how many times False appears in a row.

So here is how my table should look initially:

A B C D count
First row True False 0
Second row False True 0
Third row True True 0
Fourth row False False 0

This is how it should look:

A B C D count
First row True False 1
Second row False True 1
Third row True True 0
Fourth row False False 2

This is my code, I have tried to count for at least one column to begin with something, but it does not change the value in count column.

import pandas as pd

data = {'A': ['One', None, 'One', None], 'B': [None, 'Two', None, 'Two'], 'C': [True, False, True, False],
        'D': [False, True, True, False], 'count': [0, 0, 0, 0]}
df = pd.DataFrame(data)
for index, row in df.iterrows():
        if row['C'] is False:
                row['count']  = 1
print(df.head(4))

CodePudding user response:

IIUC, you want to count the False (boolean) values per row?

You can subset the boolean columns with select_dtypes, then invert the boolean value with ~ (so that False becomes True and is equivalent to 1), then sum per row:

df['count'] = (~df.select_dtypes('boolean')).sum(axis=1)

output:

      A     B      C      D  count
0   One  None   True  False      1
1  None   Two  False   True      1
2   One  None   True   True      0
3  None   Two  False  False      2

CodePudding user response:

Select columns 'C' and 'D', flip/invert the booleans (~) and then sum across both columns:

df['count'] = (~df[['C', 'D']]).sum(axis='columns')
  • Related