简体   繁体   中英

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:

矩阵

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM