答案 0 :(得分:1)
一种方法是使用groupby并创建一个新的数据框,然后与主数据框组合。参见下面的示例
df1 = pd.DataFrame({'cat':["Delivery" ,"Gardending", "cleaning","Delivery","Marketing"],'id':[ 2, 2,2,3,3]})
df1.head()
结果
cat id
0 Delivery 2
1 Gardending 2
2 cleaning 2
3 Delivery 3
4 Marketing 3
然后创建此
all_cat = df1.groupby('id')['cat'].apply(lambda x: pd.unique(x.values)).rename("All_cat").reset_index()
all_cat.head()
将导致
id All_cat
0 2 [Delivery, Gardending, cleaning]
1 3 [Delivery, Marketing]
然后将这两个数据框与
合并df2 = df1.merge(all_cat)
df2.head()
将导致
cat id All_cat
0 Delivery 2 [Delivery, Gardending, cleaning]
1 Gardending 2 [Delivery, Gardending, cleaning]
2 cleaning 2 [Delivery, Gardending, cleaning]
3 Delivery 3 [Delivery, Marketing]
4 Marketing 3 [Delivery, Marketing]