在Python中按ID按组折叠文本

时间:2017-03-17 20:54:45

标签: python

我有3个变量的数据集,我想创建一个新变量或更新旧的变量1 * <return value of np.all or np.allclose>,如果groupby KeyQuestion重复,则粘贴元素。

Value

我想要输出

df = pd.DataFrame({"Question":["Q008D02","Q008D02","Q008D02","Q008D02","Q008D03","Q008D03","Q008D03","Q008D03","Q008D03","Q008D04","Q008D04","Q008D04","Q008D04","Q008D04","Q008D04","Q008D04"],
               "Value" :[1,2,3,4,1,2,3,4,4,1,2,2,3,4,4,4],"Key": ["Text 1","Text 2","Text 3","Text 4","Answer1","Answer2","Answer3","Answer4","Answer5","Check1","Check2","Check3","Check4","Check5","Check6","Check7"]},
             columns = ["Question","Value","Key"])
   Question  Value      Key
0   Q008D02      1   Text 1
1   Q008D02      2   Text 2
2   Q008D02      3   Text 3
3   Q008D02      4   Text 4
4   Q008D03      1  Answer1
5   Q008D03      2  Answer2
6   Q008D03      3  Answer3
7   Q008D03      4  Answer4
8   Q008D03      4  Answer5
9   Q008D04      1   Check1
10  Q008D04      2   Check2
11  Q008D04      2   Check3
12  Q008D04      3   Check4
13  Q008D04      4   Check5
14  Q008D04      4   Check6
15  Q008D04      4   Check7

1 个答案:

答案 0 :(得分:1)

您可以使用groupby.agg,并指定','.join作为聚合函数,将来自同一组的Key粘贴在一起:

df.groupby(['Question', 'Value']).agg({'Key':','.join}).reset_index()

enter image description here