lundi 23 avril 2018

Python Pandas conditional flagging

Working with python pandas dataframe df:

product_id |transaction_id | category | color
234          54              A           black
349          54              B           silver
213          46              A           silver
490          46              A           black
245          87              A           black
249          87              B           black
294          87              A           silver

I want to flag transaction_IDs that have category of A and B with the same color. So in the scenario above transaction 87 has a product A black and a product B black.

Desired output:

product_id |transaction_id | category | color  | flag
234          54              A           black
349          54              B           silver
213          46              A           silver
490          46              A           black
245          87              A           black    X
249          87              B           black    X
294          87              A           silver   X

I was trying to create a unique key between category and color and then groupby, but it got messy and I still have to go manually through it. There must be a simpler way.

df['key']=df['category']&df['color']

df['transaction_analysis']= df.groupby('transaction_id').key.transform(lambda x : '&'.join(set(x)))

Aucun commentaire:

Enregistrer un commentaire