Finding count of distinct elements in DataFrame in each column

前端 未结 8 1173
半阙折子戏
半阙折子戏 2020-12-02 18:27

I am trying to find the count of distinct values in each column using Pandas. This is what I did.

import pandas as pd
import numpy as np

# Generate data.
NR         


        
8条回答
  •  隐瞒了意图╮
    2020-12-02 18:54

    Adding the example code for the answer given by @CaMaDuPe85

    df = pd.DataFrame({'a':[0,1,1,2,3],'b':[1,2,3,4,5],'c':[1,1,1,1,1]})
    df
    
    # df
        a   b   c
    0   0   1   1
    1   1   2   1
    2   1   3   1
    3   2   4   1
    4   3   5   1
    
    
    for cs in df.columns:
        print(cs,df[cs].value_counts().count()) 
        # using value_counts in each column and count it 
    
    # Output
    
    a 4
    b 5
    c 1
    

提交回复
热议问题