Search code examples
pythongroup-bycountmultiple-columnsdistinct

Count the occurrence of one column based on another


I have a dataframe like this

System package
mac abc
mac bcd
windows bcd
mac abc

I want a code in python to get a dataframe like this

System count of distinct package
mac 2
windows 1

Solution

  • Create dataframe:

    pd.DataFrame(data={"a":["mac","mac","windows","mac",],"b": ["abc", "bcd", "bcd", "abc"]})
    

    group it:

    t.groupby('a').b.nunique()
    

    output:

        a
    mac        2
    windows    1
    Name: b, dtype: int64