亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從短語列表中查找字典中的短語,并使用找到的短語和計數創建數據框。應計算重復項

如何從短語列表中查找字典中的短語,并使用找到的短語和計數創建數據框。應計算重復項

嚕嚕噠 2022-12-20 16:19:31
phrases = ['i am good', 'going to the market', 'eating cookies']dictionary = {'http://www.firsturl.com': 'i am going to the market and tomorrow will be eating cookies',             'http://www.secondurl.com': 'tomorrow is my birthday and i shall be',              'http://www.thirdurl.com': 'i am good and will go to sleep'}如果至少有一個匹配項:預期輸出:url                             phrasecount    phrasehttp://www.firsturl.com         2              going to the market, eating cookieshttp://www.thirdurl.com         1              i am good如果所有 3 個 url 都沒有匹配項,則只返回第一次出現的 url,計數為零,預期輸出為空白短語:url                            phrasecount    phrasehttp://www.firsturl.com        0     
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

df從相應的設置初始數據幀dictionary:


df = pd.DataFrame({'urls': list(dictionary.keys()), 'strings': list(dictionary.values())})

pattern = '|'.join(phrases)

處理數據幀:


s = df.pop('strings').str.findall(pattern)

df = df.assign(phrasecount=s.str.len(), phrase=s.map(', '.join))

df = df.drop_duplicates(subset='phrasecount') if df['phrasecount'].eq(0).all() else df[df['phrasecount'].ne(0)]

結果:


# print(df)


                      urls  phrasecount                               phrase

0  http://www.firsturl.com            2  going to the market, eating cookies

2  http://www.thirdurl.com            1                            i am good


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號