I'm translating an excel formula in pandas. Where columns with specified conditions are counted and summed up row-wise. I have to count per row if a cell from the selected column satisfy the given conditions and then add the counts which satisfy the conditions.
I have dataframe:
df:
a b c
14 x1 2
17 x2 2
0 x,1 3
1 x1 1
Excel formula:
= COUNTIFS($U2,14,$X2,"x2",$W2,2)+COUNTIFS($U2,17,$X2,"x2",$W2,2)+COUNTIFS(U2,14,$X2,"x1",$W2,2)
Pandas formula:
df['counted'] = (df[(df['a']==14) & (df['b']=='x2') & (df['c']==2)].count(axis=1)) + (df[(df['a']==17) & (df['b']=='x2') & (df['c']==2)].count(axis=1)) + (df[(df['a']==14) & (df['b']=='x1') & (df['c']==2)].count(axis=1))
I get the result below from my pandas formula: df:
a b c counted
14 x1 2 NaN
17 x2 2 NaN
0 x,1 3 NaN
1 x1 1 NaN
Expected result is as shown below. Any help to get the right formula will be very much appreciated.
Expected results df:
a b c counted
14 x1 2 0
17 x2 2 1
0 x,1 3 0
1 x1 1 0