Search code examples
pythonpandasdataframegroup-bypandas-groupby

count occurrence of a value in multiple columns of a dataframe Pandas


I want to count occurrence of a string value in multiple columns in the entire dataframe, for example :

df = pd.DataFrame({'type1':['A11','B11','A11','A'],
                     'type2':['A12','C','A12','D11'],
                     'type3':['C','D11','B','D12'],
                     'type4':[np.nan,'E12','C','E']
                    })
df

       type1    type2   type3   type4 
    0   A11      A12      C      NaN     
    1   B11       C      D11     E12     
    2   A11      A12      B       C      
    3    A       D11     D12      E     

I want to be able to count each distinct value in those 4 columns, like :

value count
 A11    2
 A12    2
 C      3
 B11    1
 D11    2
 D12    1
 E12    3
 B      1
 A      1
 E      1

Solution

  • df.stack().value_counts()
    
    C      3
    A11    2
    A12    2
    D11    2
    B11    1
    E12    1
    B      1
    A      1
    D12    1
    E      1
    

    if you need the names:

    df.stack().value_counts().reset_index(name='count').rename({'index':'value'}, axis = 1)
     
      value  count
    0     C      3
    1   A11      2
    2   A12      2
    3   D11      2
    4   B11      1
    5   E12      1
    6     B      1
    7     A      1
    8   D12      1
    9     E      1