【发布时间】:2018-04-25 06:38:57
【问题描述】:
考虑如下熊猫数据框“df”和python列表“my_list”。
df =
timestamp address type
1 1 A
2 9 B
3 3 A
4 6 B
5 6 B
6 2 B
7 3 A
8 2 B
9 1 B
10 3 A
11 3 A
12 3 A
我的列表 =
[1, 2, 3]
现在我想要的是在 3 秒的 bin 中按时间戳列对数据帧进行分组,并仅在“my_list”中存在地址时计算唯一“类型”的数量。
预期的输出应如下所示:
timestamp A B
1 2 0 #One "B" ignored, because address=9 is not in my_list
4 0 1 #Two "B" ignored because address is not in "my_list
7 1 2 #Two "B" with unique addresses, and one "A"
10 1 0 #Three rows with Type="A", but addresses are is same.
请注意,时间戳值最初是时间戳格式,我们可以应用 df.groupby 和 pd.TimeGrouper 函数对 3 秒列中的行进行分组。
仅欣赏基于 Pandas (Python) 的答案。
如有任何混淆,我们深表歉意。我尽量保持简单。
-- 汗
【问题讨论】: