英文:
combine rows with the same IDs into the same row
问题
I understand your request. Here's the translated code part without any additional content:
我明白你的请求。以下是翻译好的代码部分,没有任何额外内容:
我有一个包含600行的数据集。数据有一个主要的ID=版本(Version)和第二个ID=任务(Task)。数据如下所示:
我想要更改格式,使得属于同一个版本(Version)的“任务”(Task)在同一行中,如下所示:
请注意,这只是代码的翻译部分,不包含任何其他内容。如果需要更多帮助,请告诉我。
英文:
I have a dataset with 600 rows. the data has one main ID= Version and second ID= Task. Data looks like this:
Version Task Concept Att 1 - Att 2 -
1 1 1 3 2
1 1 2 1 1
1 2 1 2 3
1 2 2 1 2
1 3 1 2 3
1 3 2 3 1
2 1 1 2 1
2 1 2 3 2
2 2 1 2 2
2 2 2 1 3
2 3 1 3 1
2 3 2 1 3
I would like to change the format, so to have "Task" which belongs to the same "Version" in the same row like this:
Version Task Concept Att 1 - Att 2 - Version Task Concept Att 1 - Att 2 -
1 1 1 3 2 1 1 2 1 1
1 2 1 2 3 1 2 2 1 2
1 3 1 2 3 1 3 2 3 1
2 1 1 2 1 2 1 2 3 2
2 2 1 2 2 2 2 2 1 3
2 3 1 3 1 2 3 2 1 3
I have tried different things like groupby, pivot but I cannot find the right solution
答案1
得分: 0
I think a pivot
is the clean way to reshape (df.pivot(index=['Version', 'Task'], columns='Concept')
, optionally with flattening the columns MultiIndex).
That said if you really want to duplicate the columns, you could combine a groupby
and concat
:
out = (pd.concat([g.set_index(['Version', 'Task'], drop=False)
for k, g in df.groupby('Concept')], axis=1)
.reset_index(drop=True)
)
Output:
Version Task Concept Att 1 - Att 2 - Version Task Concept Att 1 - Att 2 -
0 1 1 1 3 2 1 1 2 1 1
1 1 2 1 2 3 1 2 2 1 2
2 1 3 1 2 3 1 3 2 3 1
3 2 1 1 2 1 2 1 2 3 2
4 2 2 1 2 2 2 2 2 1 3
5 2 3 1 3 1 2 3 2 1 3
英文:
I think a pivot
is the clean way to reshape (df.pivot(index=['Version', 'Task'], columns='Concept')
, optionally with flattening the columns MultiIndex).
That said if you really want to duplicate the columns, you could combine a groupby
and concat
:
out = (pd.concat([g.set_index(['Version', 'Task'], drop=False)
for k, g in df.groupby('Concept')], axis=1)
.reset_index(drop=True)
)
Output:
Version Task Concept Att 1 - Att 2 - Version Task Concept Att 1 - Att 2 -
0 1 1 1 3 2 1 1 2 1 1
1 1 2 1 2 3 1 2 2 1 2
2 1 3 1 2 3 1 3 2 3 1
3 2 1 1 2 1 2 1 2 3 2
4 2 2 1 2 2 2 2 2 1 3
5 2 3 1 3 1 2 3 2 1 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论