Erick
Erick

Reputation: 103

Python Pandas Equivalent to Excel Countif

I have tried different solutions where similar questions where made, but didn't get the expected result, so far.

I have two dataframes, df1 and df2, where both of them contain a column named 'fb rq id'.

There are far more lines in df2 than in df1, and I want to check which values from df2['fb rq id'] are in df1['fb rq id'] and how many times it appears there, creating a column on df2 with the number of times the value on that line is in df1.

If you know how to create a column with just binary information wether df2.['fb rq id'] is in df1['fb rq id'] , that also helps.

Thank you!!

Upvotes: 0

Views: 1004

Answers (1)

BGHV
BGHV

Reputation: 102

how about this?

df2['count_from_df1'] = [list(df1['fb rq id']).count(id) 
                         if id in df1['fb rq id'] else 0 
                         for id in df2['fb rq id']]

Upvotes: 1

Related Questions