numpy 如何获取pandas列中的match值?

7vhp5slm  于 2023-04-12  发布在  其他
关注(0)|答案(3)|浏览(155)

我想将pandas列中的列表与另一个普通列表进行比较,找到匹配值并将其放入另一列中。
我有一个术语列表,想查找是否有与特定单词匹配的词
| meta|
| --------------|
| ['Home',' grocery','cake']|
| ['Home',' grocery','Biscuit','Oreo']|
我正在从这个列表中查找匹配项:terms = ['cake','biscuit']
预期输出:
| meta|结果|匹配值|
| --------------|--------------|--------------|
| ['Home',' grocery','cake']|真的|['cake']|
| ['Home',' grocery','Biscuit','Oreo']|真的|['饼干']|
PS:我正在看如何获取match value

nwo49xxi

nwo49xxi1#

df = pd.DataFrame([
    {"meta": ['Home', 'grocery', 'cake']},
    {"meta": ['Home', 'grocery', 'Biscuit', 'Oreo']}
    ])

terms = ['cake', 'biscuit']

df["match_value"] = df["meta"].apply(lambda row: list(set(map(str.lower, row)).intersection(set(terms))))
nafvub8i

nafvub8i2#

你可以使用正则表达式模式在分解列表后提取子字符串:

import re

pattern = re.compile(f"({'|'.join(terms)})", re.IGNORECASE)

df['match value'] = (df['meta'].explode().str.extractall(pattern)[0]
                               .groupby(level=0).agg(list)

df['result'] = df['match value'].str.len().astype(bool)

输出:

>>> df
                             meta   match value  result
0  [Home, grocery, cake, biscuit]        [cake]    True
1  [Home, grocery, Biscuit, Oreo]     [Biscuit]    True
q1qsirdb

q1qsirdb3#

我们使用带有矢量化字符串匹配的explode

exploded = df['meta'].explode().str.lower()
df['match'] = exploded[exploded.isin(terms)].groupby(level=0).agg(list)
df['result'] = df['match'].str.len() > 0

为获得最佳结果,请确保terms中的术语也是小写的。
让我们尝试另一组术语,看看这是如何工作的

terms = ['home', 'biscuit']

exploded = df['meta'].explode().str.lower()
df['match'] = exploded[exploded.isin(terms)].groupby(level=0).agg(list)
df['result'] = df['match'].str.len() > 0
df
    
                             meta            match  result
0           [Home, grocery, cake]           [home]    True
1  [Home, grocery, Biscuit, Oreo]  [home, biscuit]    True

相关问题