Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas COUNTIF based on column value

I am trying to essentially do a COUNTIF in pandas to count how many items in a row match a number in the first column.

Dataframe:

a b c d
1 2 3 1
2 3 4 2
3 5 6 3

So I want to count instances in a row (b,c,d) that match a. In row 1 for instance it should be 1 as only d matches a.

I have searched quite a bit for this but so far only found examples where its a common number (like counting all values more than 0) but not based on a dataframe column. i'm guessing its some form of logic that masks based on the column but df == df.a doesnt seem to work

like image 265
kmccarty Avatar asked Dec 23 '22 19:12

kmccarty


1 Answers

You can use eq, which you can pass an axis parameter to specify the direction of the comparison, then you can do a row sum to count the number of matched values:

df.eq(df.a, axis=0).sum(1) - 1

#0    1
#1    1
#2    1
#dtype: int64
like image 109
Psidom Avatar answered Dec 26 '22 18:12

Psidom