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
涉及的知識點
-
python如何将兩個字典合并?
dict(dic1, **dic2)
- 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)