简体   繁体   English

如何根据 pandas 中其他两列的唯一组合获得两列的唯一计数

[英]How to get unique count of two columns based on unique combination of other two columns in pandas

This is a follow up question for this这是一个后续问题

Say I have this dataset:假设我有这个数据集:

dff = pd.DataFrame(np.array([["2020-11-13",3,4, 0,0], ["2020-10-11", 3,4,0,1], ["2020-11-13", 1,4,1,1],
                         ["2020-11-14", 3,4,0,0], ["2020-11-13", 5,4,0,1], 
                         ["2020-11-14", 2,4,1,1],["2020-11-12", 1,4,0,1],["2020-11-14", 2,4,0,1],["2020-11-15", 5,4,1,1],
                         ["2020-11-11", 1,2,0,0],["2020-11-15", 1,2,0,1],
                         ["2020-11-18", 2,4,0,1],["2020-11-17", 1,2,0,0],["2020-11-20", 3,4,0,0]]), columns=['Timestamp', 'Name', "slot", "A", "B"])

I want to have a count for each Name and slot combination but disregard multiple timeseries value for same combination of A and B .我想对每个Nameslot组合进行计数,但忽略AB的相同组合的多个时间序列值。 For example, if I simply group by Name and slot I get:例如,如果我只是按Nameslot分组,我会得到:

dff.groupby(['Name', "slot"]).Timestamp.count().reset_index(name="count")


  Name slot count
    1   2   3
    1   4   2
    2   4   3
    3   4   4
    5   4   2

However, for A == 0 && B == 0 , there are two combinations for name == 1 and slot == 2 , so instead of 3 I want the count to be 2 .但是,对于A == 0 && B == 0name == 1slot == 2有两种组合,所以我希望计数为2而不是3

This is the table I would ideally want.这是我理想中想要的桌子。

  Name slot count
    1   2   2
    1   4   2
    2   4   2
    3   4   2
    5   4   2

I tried:我试过了:

filter_one = dff.groupby(['A','B']).Timestamp.transform(min)
dff1 = dff.loc[dff.Timestamp == filter_one]
dff1.groupby(['Name', "slot"]).Timestamp.count().reset_index(name="count")

But this gives me:但这给了我:

  Name slot count
    1   2   1
    1   4   1
    3   4   1

It also does not work if I drop duplicates for A and B .如果我删除AB的重复项,它也不起作用。

If I understand correctly, you may just need to drop the duplicates based on the combination of the grouper columns with A and B before grouping:如果我理解正确,您可能只需要在分组之前根据 grouper 列与 A 和 B 的组合删除重复项:

u = dff.drop_duplicates(['Name','slot','A','B'])
u.groupby(['Name', "slot"]).Timestamp.count().reset_index(name="count")

  Name slot  count
0    1    2      2
1    1    4      2
2    2    4      2
3    3    4      2
4    5    4      2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM