简体   繁体   中英

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

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
df.apply(lambda x: (x == x[0]).sum()-1,axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM