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))

下面我们看看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

其中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_type = Check(dict(self.GetKeyWords(), **self.GetRecall()), 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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值