如何在python中过滤数据 [英] How to filter data in python

查看:301
本文介绍了如何在python中过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像d1这样的数据集.

I have a data set like d1.

import pandas as pd
d1 = {
'customers': pd.Series([1, 1, 1, 2, 2, 3, 3, 4, 4]),
'channel': pd.Series(['a', 'a', 'b', 'c', 'a', 'a', 'b', 'b', 'c']),
'freq': pd.Series([3, 3, 3, 2, 2, 2, 2, 2, 2])
}
d1=pd.DataFrame(d1)

我想获取仅使用两个不同渠道的客户列表,并且渠道"a"是必填项.

I want to get list of customers who have used only two distinct channels and channels 'a' is mandatory.

例如,第一个客户使用了两个不同的渠道"a"和'b'
第二个客户使用了'a'& 'c'第三位客户使用'a'& 'b'.但是客户4尚未使用渠道"a",依此类推....

For ex.. 1st customer has used two distinct channels 'a'& 'b'
2nd customer had used 'a' & 'c' 3rd customer used 'a' & 'b'. But customer 4 has not used channel 'a' and so on....

推荐答案

这是您想要的吗?

d1[d1.customers.isin(d1[(d1.freq==2) & (d1.channel=="a")].customers)]

  channel  customers  freq
3       c          2     2
4       a          2     2
5       a          3     2
6       b          3     2

HTH

这篇关于如何在python中过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆