我有一个交易的数据框架:
id | type | date
453| online | 08-12-19
453| instore| 08-12-19
453| return | 10-5-19有4种可能的类型: online,instore,return,other。我想要创建布尔列,在这些列中,我可以看到每个唯一的客户是否具有给定的事务类型。
我尝试了下面的代码,但它没有给我想要的东西。
transactions.groupby('id')['type'].transform(lambda x: x == 'online') == 'online' 发布于 2020-04-06 21:31:07
为每个组的indicaro列使用带有聚合max的get_dummies,最后为自定义订单添加DataFrame.reindex,并添加由0填充的可能的misisng类型
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 0join per groups和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)https://stackoverflow.com/questions/61060877
复制相似问题