Python分组并合并所有Tex

2024-05-18 05:12:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中处理NLP项目有没有一种方法可以将下面的所有反馈按特定的问题组分组?你知道吗

Out[40]:
           Issue Group           Feedback   
        24 Accessories           Nope, just make a longer charging cord :)
        49 Accessories           Everything was very helpful and nice handled
      1003 Connectivity          kEEP DOING WHAT YOU ARE DOING.
      2003 Connectivity          None! Keep up the good work!

Desired Result will be: 

           Issue Group            Feedback
           Accessories            Nope, just make a longer charging cord :) Everything was very helpful and nice handled
           Connectivity           kEEP DOING WHAT YOU ARE DOING None! Keep up the good work!



Tags: makegroupissuefeedbackveryjusteverythingdoing
2条回答

你可以试试groupby

df.groupby('Issue Group').agg(lambda x: ','.join(x))

输出的文本用逗号分隔

Nope, just make a longer charging cord :),Everything was very helpful and nice handled
kEEP DOING WHAT YOU ARE DOING,None! Keep up the good work!

如果要在输出中添加列表

df.groupby('Issue Group').agg(list)

其输出形式如下:

['Nope, just make a longer charging cord :)', 'Everything was very helpful and nice handled']
['kEEP DOING WHAT YOU ARE DOING', 'None! Keep up the good work!']

举个例子:

import pandas as pd

text = [ ('Accessories','Nope, just make a longer charging cord :)') ,
             ('Accessories','Everything was very helpful and nice handled' ) ,
             ('Connectivity','kEEP DOING WHAT YOU ARE DOING'),
            ('Connectivity','None! Keep up the good work!') ]

df = pd.DataFrame(text, columns = ['Col1' , 'Col2']) 
print(pd.pivot_table(df,index=['Col1'],values='Col2',aggfunc=lambda x: ','.join(x)))

相关问题 更多 >