+4 votes
in Programming Languages by (56.5k points)
I want to find the number of elements in a Pandas DataFrame that are greater than a given value 'k'. Which function should I use for it?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

There can be several ways to find the number of elements greater than a value in a DataFrame. One way is to use the count() function that returns the number of non-NA cells for each column or row.
If you do df[df>k], you will get a new dataframe with NaN for cells that are less than 'k'. You can then apply the count() function to the new dataframe.

Here is an example:

>>> import numpy as np
>>> import pandas as pd
>>> np.random.seed(7)
>>> df = pd.DataFrame({'a':np.random.randint(5,50,10), 'b':np.random.randint(5,50,10)})
>>> df
    a   b
0   9  30
1  30  47
2   8  31
3  24  13
4  28  44
5  44  43
6  33   9
7  19  12
8  28  49
9  13   5
>>> df1=df[df>10]
>>> df1
      a     b
0   NaN  30.0
1  30.0  47.0
2   NaN  31.0
3  24.0  13.0
4  28.0  44.0
5  44.0  43.0
6  33.0   NaN
7  19.0  12.0
8  28.0  49.0
9  13.0   NaN
>>> df1.count()
a    8
b    8
 


...