I have a dataframe:
id group
x1 A
x1 B
x2 A
x2 A
x3 B
I would like to create a new column new_group with the following conditions:
If there are 2 unique group values within in the same id such as group A and B from rows 1 and 2, new_group should have "two" as its value. If there are only 1 unique group values within the same id such as group A from rows 3 and 4, the value for new_group should be that same group A. Otherwise, specify B.
This is what I am looking for:
id group new_group
x1 A two
x1 B two
x2 A A
x2 A A
x3 B B
I tried something like this but don't know how to capture all the if-else conditions
df.groupby("id")["group"].filter(lambda x: x.nunique() == 2)