Py.rookie89
Py.rookie89

Reputation: 129

Groupby id and create column boolean column

I have a dataframe of transactions:

  id |   type |    date
  453| online | 08-12-19
  453| instore| 08-12-19
  453| return | 10-5-19

There are 4 possible types: online, instore, return, other. I want to create boolean columns where I see if for each unique customer if they ever had a given transaction type.

I tried the following code but it was not giving me what I wanted.

transactions.groupby('id')['type'].transform(lambda x: x == 'online') == 'online' 

Upvotes: 1

Views: 116

Answers (1)

jezrael
jezrael

Reputation: 863301

Use get_dummies with aggregate max for indicaro columns per groups and last add DataFrame.reindex for custom order and add possible misisng types filled by 0:

t = ['online', 'instore', 'return', 'other']
df = pd.get_dummies(df['type']).groupby(df['id']).max().reindex(t, axis=1, fill_value=0)
print (df)
     online  instore  return  other
id                                 
453       1        1       1      0

Another idea with join per groups and Series.str.get_dummies:

t = ['online', 'instore', 'return', 'other']
df.groupby('id')['type'].agg('|'.join).str.get_dummies().reindex(t, axis=1, fill_value=0)

Upvotes: 2

Related Questions