将数据框值透视和合并到新列中

时间:2019-01-24 23:40:44

标签: python dataframe

您好,情况很简单,但通过示例更容易解释。

数据

enter image description here

我想要达到的结果 enter image description here

如您所见,对于每个ID,我在新列ALL_CATEGORY

中总结了它们所做的所有类别

我想我可以使用循环来做到这一点?

1 个答案:

答案 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]