For Pandas 1.5
I have a DataFrame like this:
| A | B | C | D |
| ----- | ----- | ----- | ----- |
| False | True | 63.56 | True |
| False | True | 57.13 | <NA> |
| True | True | 87.31 | True |
| False | <NA> | 71.09 | True |
| False | False | 11.78 | False |
| <NA> | False | 5.07 | True |
| True | False | 5.11 | True |
| False | False | 9.55 | True |
The value types of column A/B/D are nullable Boolean (True
/False
/null
).
I want to count the total numbers of True
/False
/null
in Column A/B/D for every row like this:
| A | B | C | D | TrueCount | FalseCount | NullCount |
| ----- | ----- | ----- | ----- | --------- | ---------- | --------- |
| False | True | 63.56 | True | 2 | 1 | 0 |
| False | True | 57.13 | <NA> | 1 | 1 | 1 |
| True | True | 87.31 | True | 3 | 0 | 0 |
| False | <NA> | 71.09 | True | 1 | 1 | 1 |
| False | False | 11.78 | False | 0 | 3 | 0 |
| <NA> | False | 5.07 | True | 1 | 1 | 1 |
| True | False | 5.11 | True | 2 | 1 | 0 |
| False | False | 9.55 | True | 1 | 2 | 0 |
Is there any method/way similar to COUNTIF()
in Excel which could let me write code like this pseudo-code: df['TrueCount'] = df.COUNTIF(columns=['A','B','D'], True)
.
P.S. I have large data (100k+ rows), so I also need to care the performance.
>Solution :
You can just sum across axis=1
df['TrueCount'] = df[['A', 'B', 'D']].sum(axis=1)
df['FalseCount'] = (df==False).sum(axis=1)
df['NullCount '] = df.isna().sum(axis=1)