如何查看一个值在熊猫的1列中是否有2个匹配项

问题描述

我有需要评估的A / B测试结果,但是在检查数据时,我注意到两个对照组中都有用户,因此我需要丢弃它们以免损害测试。我的数据看起来像这样:

    transactionId   visitorId   date       revenue  group
0   906125958          0        2019-08-16  10.8     B
1   1832336629         1        2019-08-04  25.9     B
2   3698129301         2        2019-08-01  165.7    B
3   4214855558         2        2019-08-07  30.5     A
4   797272108          3        2019-08-23  100.4    A

我需要做的是删除A组和B组中的每个用户,同时保持其余所有用户不变。因此,从示例数据中,我需要以下输出

    transactionId   visitorId   date       revenue  group
0   906125958          0        2019-08-16  10.8     B
1   1832336629         1        2019-08-04  25.9     B
4   797272108          3        2019-08-23  100.4    A

我尝试了各种方式,但似乎无法解决,也找不到任何答案,在这里我真的很感谢您的帮助, 预先感谢

解决方法

您可以像这样获得一组用户的列表:

group_counts = df.groupby('visitorId').agg({'group': 'nunique'}) ##list of users with number of groups
to_include = group_counts[group_counts['group'] == 1] ##filter for just users in 1 group

然后根据该列表中的访问者来过滤原始数据:

df = df[df['visitorId'].isin(to_include.index)]