根据另一列计算一列的出现次数

Count the occurrence of one column based on another

我有一个这样的数据框

System package
mac abc
mac bcd
windows bcd
mac abc

我想要 python 中的代码来获得这样的数据框

System count of distinct package
mac 2
windows 1

创建数据框:

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

分组:

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

输出:

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