samthebrand
samthebrand

Reputation: 3090

How to count occurrences of a set of values for a range of columns in a pandas dataframe?

I have a pandas dataframe that looks something like this:

matrix

The dataframe is populated with 4 distinct strings: '00', '01', '10',and '11'. I'm hoping to count each occurrence of the values in each column, so that the data above would return a resulting dataframe that looks something like this:

    A   B   C   D   E
00  2   1   3   0   3
01  2   2   0   2   1
10  0   0   1   2   0
11  1   2   1   1   1

The original dataframe can be created with this code:

dft = pd.DataFrame({'A' : ['11', '01', '01', '00', '00'],
                   'B' : ['00', '01', '11', '01', '11'],
                   'C' : ['00', '00', '10', '00', '11'],
                   'D' : ['10', '01', '11', '10', '01'],
                   'E' : ['00', '01', '00', '11', '00'],})
dft

Upvotes: 1

Views: 1485

Answers (1)

Alexander
Alexander

Reputation: 109526

You can use value_counts together with a dictionary comprehension to generate the values, and then use the data to create a DataFrame.

>>> pd.DataFrame({col: dft[col].value_counts() for col in dft}).fillna(0)
    A  B  C  D  E
00  2  1  3  0  3
01  2  2  0  2  1
10  0  0  1  2  0
11  1  2  1  1  1

Upvotes: 2

Related Questions