I have a data frame:
A B C D E
12 4.5 6.1 BUY NaN
12 BUY BUY 5.6 NaN
BUY 4.5 6.1 BUY NaN
12 4.5 6.1 0 NaN
I want to count the number of times 'BUY' appears in each row. Intended result:
A B C D E score
12 4.5 6.1 BUY NaN 1
12 BUY BUY 5.6 NaN 2
15 4.5 6.1 BUY NaN 1
12 4.5 6.1 0 NaN 0
I have tried the following but it simply gives 0 for all the rows:
df['score'] = df[df == 'BUY'].sum(axis=1)
Note that BUY can only appear in B, C, D, E columns.
I tried to find the solution online but shockingly found none.
Little help will be appreciated. THANKS!