'Summarising results of multiple Pandas Boolean columns
I have a Pandas dataset with multiple Boolean columns (effectively flagging what type of sustainability targets a company has set). I would like to summarise the results of these these columns by counting the number of True and False occurances.
A simplified example of the dataset:
data1 = [['Apple', True, True, True], ['Tesla', True, False, True], ['Nike', True, False, False]]
df1 = pd.DataFrame(data1, columns=['Company', 'Carbon-target', 'Water-target', 'Waste-target'])
Company Carbon-target Water-target Waste-target
0 Apple True True True
1 Tesla True False True
2 Nike True False False
Desired output:
True False
Carbon-target 3 0
Water-target 1 2
Waste-target 2 1
Currently, the only operator I can think of is the word_count function, but would require several iterations. Is there a simpler way?
Thank you !
Solution 1:[1]
Select boolean columns
x = df.select_dtypes(bool)
Count True and False values
out = pd.DataFrame({True: x.sum(), False: df.shape[0] - x.sum()})
Output
True False
Carbon-target 3 0
Water-target 1 2
Waste-target 2 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vishnudev |
