1

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!

0

4 Answers 4

7

You can compare and then sum:

df['score'] = (df[['B','C','D','E']] == 'BUY').sum(axis=1)

This sums up all the booleans and you get the correct result.


When you do df[df == 'BUY'], you are just replacing anything which is not BUY with np.nan and then taking sum over axis=1 doesnot work since all you have left in your result is np.nan and the 'BUY' string. Hence you get all 0.

Sign up to request clarification or add additional context in comments.

Comments

2

Or you could use apply with list.count:

df['score'] = df.apply(lambda x: x.tolist().count('BUY'), axis=1)
print(df)

Output:

     A    B    C    D   E  score
0   12  4.5  6.1  BUY NaN      1
1   12  BUY  BUY  5.6 NaN      2
2  BUY  4.5  6.1  BUY NaN      2
3   12  4.5  6.1    0 NaN      0

Comments

1

Try using apply with lambda over axis=1. This picks up each row at a time as a series. You can use the condition [row == 'BUY'] to filter the row and then count the number of 'BUY' using len()

df['score'] = df.apply(lambda row: len(row[row == 'BUY']), axis=1)
print(df)
     A    B    C    D   E  score
0   12  4.5  6.1  BUY NaN      1
1   12  BUY  BUY  5.6 NaN      2
2  BUY  4.5  6.1  BUY NaN      2
3   12  4.5  6.1    0 NaN      0

Comments

1
import numpy as np
df['score'] = np.count_nonzero(df == 'BUY', axis=1)

Output:

      A   B   C   D   E score
0    12 4.5 6.1 BUY NaN     1
1    12 BUY BUY 5.6 NaN     2
2   BUY 4.5 6.1 BUY NaN     2
3    12 4.5 6.1   0 NaN     0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.