What is the simplest way to compare two columns using groupby - without apply/lambda?
df product1 product2 person
0 apple apples abby
1 apple apple abby
...
df.groupby("person").product1.eq(df.groupby("person").product2)
Expected Output
0 False
1 True
CodePudding user response:
A bit tricky to solve without apply
but you can use merge
to compare ('person', 'product2) to ('person', 'product1'):
idx = (df.rename(columns={'product2': 'product'})[['person', 'product']].reset_index()
.merge(df.rename(columns={'product1': 'product'})[['person', 'product']],
on=['person', 'product'])['index'].unique())
df['compare'] = df.index.isin(idx)
print(df)
# Output
product1 product2 person compare
0 apple apples abby False
1 apple apple abby True