Skip to content Skip to sidebar Skip to footer

Pandas Count Number Of Elements In Each Column Less Than X

I have a DataFrame which looks like below. I am trying to count the number of elements less than 2.0 in each column, then I will visualize the result in a bar plot. I did it using

Solution 1:

In [96]:

df = pd.DataFrame({'a':randn(10), 'b':randn(10), 'c':randn(10)})
df
Out[96]:
          a         b         c
0 -0.849903  0.944912  1.285790
1 -1.038706  1.445381  0.251002
2  0.683135 -0.539052 -0.622439
3 -1.224699 -0.358541  1.361618
4 -0.087021  0.041524  0.151286
5 -0.114031 -0.201018 -0.030050
6  0.001891  1.601687 -0.040442
7  0.024954 -1.839793  0.917328
8 -1.480281  0.079342 -0.405370
9  0.167295 -1.723555 -0.033937

[10 rows x 3 columns]
In [97]:

df[df > 1.0].count()

Out[97]:
a    0
b    2
c    2
dtype: int64

So in your case:

df[df < 2.0 ].count() 

should work

EDIT

some timings

In [3]:

%timeit df[df < 1.0 ].count() 
%timeit (df < 1.0).sum()
%timeit (df < 1.0).apply(np.count_nonzero)
1000 loops, best of 3: 1.47 ms per loop
1000 loops, best of 3: 560 us per loop
1000 loops, best of 3: 529 us per loop

So @DSM's suggestions are correct and much faster than my suggestion


Post a Comment for "Pandas Count Number Of Elements In Each Column Less Than X"