将行与一列中的重复列入合并,但倍数在另一列中差异差异

发布于 2025-02-06 12:47:09 字数 1625 浏览 0 评论 0原文

我有一个带有问题,数字和含义的标题的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2025-02-13 12:47:09

一种简单的方法是创建一个多索引:

df = pd.DataFrame({'Q':['A', 'A', 'A', 'B', 'B','C'], 'join':[1,2,3,4,5,6]})
df.set_index(['Q', 'join'])

输出:

Q   join
A   1
    2
    3
B   4
    5
C   6

这只是使用更简单的数据框架作为示例,相同的代码(df.set_index(['QUADICY','join','join'])可以很容易地在应用于您的。

A simple way is to create a multiindex:

df = pd.DataFrame({'Q':['A', 'A', 'A', 'B', 'B','C'], 'join':[1,2,3,4,5,6]})
df.set_index(['Q', 'join'])

Output:

Q   join
A   1
    2
    3
B   4
    5
C   6

This just uses a simpler dataframe as an example, the same code (df.set_index(['Question', 'join'])) can easily be applied to yours.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文