I have the following data frame:
dataset = {
'ID':[1,1,1,1,2,2,2,2],
'Animal': ["cat","bat","cat","bat","monkey","monkey","bird","bird"],
'Bool':[True,False,False,False,True,False,False,False],}
df = pd.DataFrame(dataset)
df.groupby(["ID"])
I would like to select for each grouped subset by ID, the row that has the column df["Animal"] equal to df["Animal"] from another row where df["Bool"] is True.
The results would be stored in df["IS_Same"] as shown in the expected output.
Expected output:
ID Animal Bool Is_Same
0 1 cat True False
1 1 bat False False
2 1 cat False True
3 1 bat False False
4 2 monkey True False
5 2 monkey False True
6 2 bird False False
7 2 bird False False
Is there a way to do this easily without using a loop ?