mpy
mpy

Reputation: 632

Count the number of column for each rows of a pandas where a condition holds

I have a Pandas dataframe as follow:

data = pd.DataFrame({'w1':[0,1,0],'w2':[5,8,0],'w3':[0,0,0],'w4' :[5,1,0], 'w5' : [7,1,0],'condition' : [5,1,0]})

enter image description here

I need to have a column that for each row,counts the number of columns( columns other than "condition") which their values are equal to "condition". The final output should look like bellow:

enter image description here

I don't want to write a for loop.

As a solution,I wanted to replace the values which are equal to the "condition" with 1 and others with 0 by np.where as bellow, and then sums the 1s of each row, which was not helpful:

data = pd.DataFrame(np.where(data.loc[:,data.columns != 'condition'] == data['condition'], 1, 0), columns = data.columns)

That was just an idea (I mean replacing the values with 1 and 0) but any pythonic solution isappreciated.

Upvotes: 1

Views: 387

Answers (1)

jezrael
jezrael

Reputation: 862511

Compare all columns without last by column condition with DataFrame.eq and count Trues by sum:

data['new'] = data.iloc[:, :-1].eq(data['condition'], axis=0).sum(axis=1)

Another idea is compare all columns with remove condition col:

data['new'] = data.drop('condition', axis=1).eq(data['condition'], axis=0).sum(axis=1)

Thank you for comment @Sayandip Dutta, your idea is compare all columns and remove 1:

data['new'] = data.eq(data['condition'], axis=0).sum(axis=1).sub(1)

print (data)
   w1  w2  w3  w4  w5  condition  new
0   0   5   0   5   7          5    2
1   1   8   0   1   1          1    3
2   0   0   0   0   0          0    5

Upvotes: 5

Related Questions