Reputation: 1
df_con = df[['Traffic_Signal', 'Crossing', 'Junction', 'Bump', 'Railway']]
I have a data frame of boolean values. I want to create a count of total cases where any of the above columns are also True.
I started looping through the df with iterrows, but I'm getting errors I'm unfamiliar with...any guidance is appreciated!
This was my closest (I think) attempt:
df_con = df[['Traffic_Signal', 'Crossing', 'Junction', 'Bump', 'Railway']]
cols = ['Traffic_Signal', 'Crossing', 'Junction', 'Bump', 'Railway']
count = 0
for index, row in df_con.iterrows():
for col in cols:
if df_con.loc[col] == True:
count += 1
break
print(count)
Upvotes: 0
Views: 454
Reputation: 329
I'd recommend that you use the .any()
method to get a boolean for that row and then .sum()
that result to get your final count.
Here is a very simple example using my own columns and data.
import pandas as pd
df = pd.DataFrame(
{
"excluded": [0, 1, 2, 3],
"x": [True, True, True, False],
"y": [True, False, False, False],
"z": [True, True, False, False],
}
)
dff = df[["x", "y", "z"]]
count = dff.any(axis="columns").sum()
print(count) # Gives expected result of 3
Upvotes: 1