'Groupby A column and bring up the A value, only if the B values differ from the other ones, including nulls

I have this example, dataset:

 A   B 
11   A
11   V
11   C
12   A
12   A
12   A
12   A
13   A
13   A
13   B
13   B
14   C
14   C
14   
14

And I want it to return, the grouped A values, that has different values on the B column. So in this example, the expected output is:

[11, 13,14]

I made an attempt at formulating the code, and I succeeded but it is terrible and unoptimized. And I was looking for alternatives so I could iterate faster through my much bigger dataset. Would appreciate some help.

Here is my code:

user_mult_camps = []

for i in  df['A'].unique():
    filt = (df['A'] == i)
    df2 = df.loc[filt]
    x=df2['B'].unique()
    if len(x) > 1:
        user_mult_camps.append(i)
        print(i)


Solution 1:[1]

Try this:

out = df.groupby('A')['B'].nunique().pipe(lambda x: x[x > 1].index.to_numpy())

Output:

>>> out
array([11])

>>> out[0]
11

Solution 2:[2]

IIUC, you want to ensure all values are different?

You could use:

s = df.groupby('A')['B'].apply(lambda s: ~s.duplicated().any())

s[s].index.tolist()

Output: [11]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2