我有以下示例集。
CustID Condition Month Reading Consumption
0 108000601 True June 20110606 28320.0
1 108007000 True July 20110705 13760.0
2 108007000 True August 20110804 16240.0
3 108008000 True September 20110901 12560.0
4 108008000 True October 20111004 12400.0
5 108000601 False November 20111101 9440.0
6 108090000 False December 20111205 12160.0
7 108008000 False January 20120106 11360.0
8 108000601 True February 20120206 10480.0
9 108000601 True March 20120306 9840.0
以下 groupby 为我提供了我正在寻找的部分内容。
dfm.groupby(['条件'])['CustID'].nunique()
Condition
True 3
False 3
但是我如何获得符合这两个条件的唯一 ID?例如
Condition
True 3
False 3
Both 2
最佳答案
不确定这是否是最“ Pandas ”的方式,但您可以使用set
来比较每个分区中的用户(Python set
数据结构是一个散列表将自动丢弃重复项):
custid_true = set(dfm[dfm['Condition']==True].CustID)
custid_false = set(dfm[dfm['Condition']==False].CustID)
custid_both = custid_true.intersection(custid_false)
n_custid_both = len(custid_both)
关于python - pandas:groupby 两列 nunique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215467/