I have the following sample data
df1 = [[52, '1', '10'], [54, '1', '4'],
[55, '2', '3'], [52, '1', '10'],
[55, '2', '10'], [52, '1', '4']]
df = pd.DataFrame(df1, columns =['Cow', 'Lact', 'Procedure'])
df2 = [['52', '1'], ['53', '9'],
['54', '2'], ['55', '2']]
df2 = pd.DataFrame(df2, columns =['Cow', 'Lact'])
The tables look like:
df:
Cow Lact Procedure
0 52 1 10
1 54 1 4
2 55 2 3
3 52 1 10
4 55 2 10
5 52 1 4
df2:
Cow Lact
0 52 1
1 53 9
2 54 2
3 55 2
I would like to count the number of procedure = 10 for each Cow-Lact combination in df2 and then add a column to df2 called Tproc that includes the count.
The output I am looking for is
Cow Lact Tproc
0 52 1 2
1 53 9 0
2 54 2 0
3 55 2 1
The following filter does not work:
filt = [(df['Cow']==df2['Cow'])&(df['Lact']==df2['Lact'])&(df['Procedure']==10)]
My plan was then to use .len to get a count
df2['Tproc'] = df2.loc[filt].len
How can you filter a DataFrame based on values in another DataFrame to count the number of rows that satisfy the condition?