首页 > 解决方案 > 如何从短语列表中查找字典中的短语并使用找到和计数的短语创建数据框。应计算重复项

问题描述

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    phrase
http://www.firsturl.com         2              going to the market, eating cookies
http://www.thirdurl.com         1              i am good

如果所有 3 个 url 均不匹配,则仅返回第一个出现的 url,计数为零且预期输出为空白短语:

url                            phrasecount    phrase
http://www.firsturl.com        0              

标签: pythonpandasdataframedictionary

解决方案


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

推荐阅读