Search code examples
python-3.xpandaspandas-groupby

To extract distinct values for all categorical columns in dataframe


I have a situation where I need to print all the distinct values that are there for all the categorical columns in my data frame The dataframe looks like this :

Gender  Function  Segment
M       IT        LE
F       IT        LM
M       HR        LE
F       HR        LM

The output should give me the following:

Variable_Name    Distinct_Count
Gender           2
Function         2
Segment          2

How to achieve this?


Solution

  • using nunique then passing the series into a new datafame and setting column names.

    df_unique = df.nunique().to_frame().reset_index()
    df_unique.columns = ['Variable','DistinctCount']
    

    print(df_unique)
       Variable  DistinctCount
    0    Gender              2
    1  Function              2
    2   Segment              2