天天看点

pandas模块化设计

pandas模块化设计

pandas对某一个字段实现功能,返回为多个字段,我们该如何实现?

背景:我们有一个字段,记录了每条通话记录,我们要对该条通话记录进行质检,将通话记录中的违规词识别出来,并且统计违规词的个数对其打分,结果分别为score(int), obeyed_words(string), type(string)

上代码

df['obeyed_words'], df['type'], df['score'] = zip(*df['content'].apply(self.identify_obeyed_words))      
def identify_obeyed_words(self, content):
        if pd.isnull(content):
            return '', '', 0
        obeyed_words = []
        ah = ac_automation()
        ah.parse(dict(self.GetKeyWords(), **self.GetRecall()))
        w1 = ah.check_words(text=content, rm_list=self.GetRecall().values())
        w2 = ah.recall_search(content, self.GetRecall())
        obeyed_words.extend(w1 + w2)
        obeyed_words = list(set(obeyed_words))
        obeyed_type = Check(dict(self.GetKeyWords(), **self.GetRecall()), obeyed_words)
        score = len(obeyed_words)
        return ",".join(obeyed_words), obeyed_type, score      

涉及的知识点

  1. python如何将两个字典合并?

    ​​

    ​dict(dic1, **dic2)​

  2. zip的操作
# 与 zip 相反,zip(*) 可理解为解压,返回组成列表的一个个的元祖
a=[1,2,3]
b=['A','B','C']
c=[4,5,6]
zip_1 = zip(a,b,c)
x,y,z=zip(*zip_1)
print(x,y,z) #(1, 2, 3) ('A', 'B', 'C')(4, 5, 6)