将行与一列中的重复列入合并,但倍数在另一列中差异差异
我有一个带有问题,数字和含义的标题的pandas dataframe,我的目标是通过插入 response_number 和 response_meaning 来创建词汇表并删除 Question 列的副本
我已经加入了 response_number 和 response_meaning 列列,并使用此代码行插入新的 join 专栏:
df['join'] = df[['response_number', 'response_meaning']].agg(' : '.join, axis=1)
这是我正在使用和完成的工作的一个示例:
Question response_number response_meaning join
sys_RespStatus 2 Uncomplete 2 : Uncomplete
sys_RespStatus 4 Disqualified 4 : Disqualified
sys_RespStatus 5 Complete 5 : Complete
Q0 1 YES 1 : YES
Q0 2 NO 2 : NO
... ... ... ...
efgtb 2 B 2 : B
efgtb 3 C 3 : C
efgtb 4 D 4 : D
Q1301B 1 YES 1 : YES
Q1301B 2 NO 2 : NO
这是我想要的:
Question glossary
sys_RespStatus 2 : Uncomplete,
4 : Disqualified,
5 : Complete
Q0 1 : YES,
2 : NO
... ...
efgtb 2 : B,
3 : C,
4 : D
Q1301B 1 : YES,
2 : NO
任何帮助,建议或提示都将受到赞赏,THX!
I have a pandas dataframe with the title of the questions, numbers and the meaning of theses numbers, my goal is to create a glossary by joigning the response_number and response_meaning columns and removing duplicates for the Question column
I have already joined the response_number and response_meaning columns with this line of code into a new join column:
df['join'] = df[['response_number', 'response_meaning']].agg(' : '.join, axis=1)
Here is an example of what I'm working with and done:
Question response_number response_meaning join
sys_RespStatus 2 Uncomplete 2 : Uncomplete
sys_RespStatus 4 Disqualified 4 : Disqualified
sys_RespStatus 5 Complete 5 : Complete
Q0 1 YES 1 : YES
Q0 2 NO 2 : NO
... ... ... ...
efgtb 2 B 2 : B
efgtb 3 C 3 : C
efgtb 4 D 4 : D
Q1301B 1 YES 1 : YES
Q1301B 2 NO 2 : NO
And here is what I would like:
Question glossary
sys_RespStatus 2 : Uncomplete,
4 : Disqualified,
5 : Complete
Q0 1 : YES,
2 : NO
... ...
efgtb 2 : B,
3 : C,
4 : D
Q1301B 1 : YES,
2 : NO
Any help, advices or hints is appreciated, thx!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种简单的方法是创建一个多索引:
输出:
这只是使用更简单的数据框架作为示例,相同的代码(
df.set_index(['QUADICY','join','join'])
可以很容易地在应用于您的。A simple way is to create a multiindex:
Output:
This just uses a simpler dataframe as an example, the same code (
df.set_index(['Question', 'join'])
) can easily be applied to yours.