Home > Blockchain >  Grouping by Id and grabbing only Ids with one type
Grouping by Id and grabbing only Ids with one type

Time:02-16

I want to get all IPs that only contain Type A. How can I do this in pandas?

IP  Type
101  A
101  A
101  A
102  A 
102  B
102  C
103  C
104  B
104  B
105  A

Data:

{'IP': [101, 101, 101, 102, 102, 102, 103, 104, 104, 105],
 'Type': ['A', 'A', 'A', 'A', 'B', 'C', 'C', 'B', 'B', 'A']}

I've tried a groupby but not sure what aggregate I should use?

Expected output:

IP  Type
101  A
101  A
101  A
105  A

CodePudding user response:

There's probably a better option, but using groupby transform('nunique') and query:

df[df.groupby('IP')['Type'].transform('nunique').eq(1)].query("Type == 'A'")

CodePudding user response:

@BigBen's answer is the most intuitive and readable answer. You could also do something like:

out = df[df.groupby('IP')['Type'].transform(lambda x: x.eq('A').sum()==len(x))]

Output:

    IP Type
0  101    A
1  101    A
2  101    A
9  105    A
  • Related