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
添加回答
举报