如何过滤 python 中的数据

How to filter data in python

我有一个像 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' 是必需的。

例如..第一个客户使用了两个不同的渠道'a'&'b'
第二位客户使用了 'a' & 'c' 第三位客户使用了 'a' & 'b'。但是客户4没有用过渠道'a'等等....

这是你想要的吗?

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