0

I have a DataFrame like this:

index column1 column2 column3
1          30      55      62
2          69      20      40
3          23      62      23
...

May I know how to count the number of values which are > 50 for all elements in the above table?

I'm trying below method:

count = 0
for column in df.items():
    count += df[df[column] > 50][column].count()

Is this a proper way to do it? Or any other more effective suggestion?

2 Answers 2

1

You can just check all the values at once and then sum() them since True evaluates to 1 and False to 0:

df.gt(50).sum().sum()
Sign up to request clarification or add additional context in comments.

Comments

0

(df > 54).values.sum() will do what you're looking for here is the total code to get the results:

>>> df = pd.DataFrame(np.random.randint(0,100,size=(5, 2)), columns=list('AB'))
>>> df
    A   B
0  68  92
1  47  53
2   5  35
3  75  82
4  51  89
>>> (df > 54).values.sum()
5
>>>

Basically what I'm doing is creating a mask of true false values of the entire data frame based on the condition in this case > 54 and then just rolling up the data frame because true/false is equal to 1/0 when added.

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.