我有一个熊猫数据帧,如下所示
key message Final Category
0 1 I have not received my gifts which I ordered ok voucher
1 2 hth her wells idyll McGill kooky bbc.co noclass
2 3 test test test 1 test noclass
3 4 test noclass
4 5 hello where is my reward points other
5 6 hi, can you get koovs coupons or vouchers here options
6 7 Hi Hey when you people will include amazon an options
我想要一个{键:{键:值},…}类型的数据结构,其中第一个按最后一个类别分组,对于每个类别,我有一个字典来表示每个单词的频率。 例如 我可以将所有的noclass分组如下{'noclass':{'test':5,'1':1,'hth':1,'her':1….},}
我是新来的,很抱歉写得不好。 谢谢
可能有更雄辩的方法来实现这一点,但这里有一堆嵌套的for循环:
这会修改原始df,所以您可能需要先复制它
此代码的作用:首先,它在所有消息的末尾添加一个空格。这个稍后再来。 然后按最后一个类别进行分组,并对每组中的消息进行汇总。这就是尾随空格很重要的地方,否则消息的最后一个单词将粘在下一个单词的第一个单词上。(求和是字符串的串联)
然后将字符串沿空格分割得到单词,然后进行计数。在
相关问题 更多 >
编程相关推荐