Home > Back-end >  pandas Count column, where value is yes
pandas Count column, where value is yes

Time:07-13

I have a dataframe table like this

First name First name Parameter 1 Parameter 1
1 1 No No
1 1 Yes No
1 1 Yes No

I need to count all rows where column Parameter 1 AND Parameter 2 is Yes

I use pandas for this, but is doesn't work.

Temp.append(list(df.loc[(df.Parameter1 == 'Yes') & (df.Parameter2== 'Yes')].count()))

Error: Cannot index with multidimensional key

How i can solve this problem?

CodePudding user response:

You can ask if a condition is satisfied for multiple columns and then check that is always satisfied in each row.

import pandas as pd

# data
df = pd.DataFrame(
    {"id": [0,1,2,3],
     "par1":["Yes", "Yes", "No", "No"],
     "par2":["Yes", "No", "Yes", "No"]})

# count how many times both par are Yes
df[["par1", "par2"]].eq("Yes").all(1).sum()

CodePudding user response:

loc only accepts scalar (for single index) / tuple (for multi index) or list like data structure as labels, not dataframe. Make sure your index is not a data frame. If it’s a data frame, convert it to a Series object first by simply accessing the column that you want to use as index.

Documentation

CodePudding user response:

df[(df['Parameter 1'] == 'Yes') & (df['Parameter 2'] == 'Yes')].sum()

CodePudding user response:

sum((temp['Parameter 1'] == 'Yes') & (temp['Parameter 1'] == 'Yes'))
  • Related