Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count occurrences of number by column in pandas data frame

Tags:

python

pandas

I have a pandas data frame I want to count how often a number appears in a column for each column

     a   b   c   d   e
0    2   3   1   5   4
1    1   3   2   5   4
2    1   3   2   5   4
3    2   4   1   5   3
4    2   4   1   5   3

This is my code that does not work

def equalsOne(x):
    x[x.columns == 1].sum()

df1.apply(equalOne(), axis = 1)

Here is the desired output

a 2
b 0
c 3
d 0
e 0 
like image 534
Erich Avatar asked Nov 26 '14 03:11

Erich


People also ask

How do you count the number of repeated values in Pandas?

You can count the number of duplicate rows by counting True in pandas. Series obtained with duplicated() . The number of True can be counted with sum() method. If you want to count the number of False (= the number of non-duplicate rows), you can invert it with negation ~ and then count True with sum() .

How do I count the number of unique values in a column in Pandas?

You can use the nunique() function to count the number of unique values in a pandas DataFrame.


2 Answers

You can do:

(df==1).sum()

df==1 gives:

       a      b      c      d      e
0  False  False   True  False  False
1   True  False  False  False  False
2   True  False  False  False  False
3  False  False   True  False  False
4  False  False   True  False  False

and the sum() treats False as 0 and True as 1.

like image 190
Daniel Avatar answered Oct 05 '22 08:10

Daniel


This should do the trick

df1[df1 == 1].count()
like image 35
Bob Haffner Avatar answered Oct 05 '22 08:10

Bob Haffner