简体   繁体   中英

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 . For example, if I simply group by Name and slot I get:

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 .

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 .

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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