【问题标题】:Match keywords in pandas column with another list of elements将 pandas 列中的关键字与另一个元素列表匹配
【发布时间】:2019-01-05 13:18:01
【问题描述】:

我有一个熊猫数据框:

word_list
['nuclear','election','usa','baseball']
['football','united','thriller']
['marvels','hollywood','spiderman']
....................
....................
....................

我还有多个带有类别名称的列表,例如:-

movies=['spiderman','marvels','thriller']'

sports=['baseball','hockey','football'],

politics=['election','china','usa'] 和许多其他类别。

我只想将 pandas 列 word_list 的关键字与我的类别列表匹配,如果关键字匹配在一起并且如果任何关键字在任何列表中不匹配,则在单独的列中分配相应的列表名称,然后简单地输入miscellaneous 所以,我正在寻找的输出为:-

word_list                                          matched_list_names
['nuclear','election','usa','baseball']            politics,sports,miscellaneous
['football','united','thriller']                   sports,movies,miscellaneous               
['marvels','spiderman','hockey']                   movies,sports

....................                               .....................
....................                               .....................
....................                               ....................

我成功获取到匹配关键字为:-

for i in df['word_list']:
    for j in movies:
        if i in j:
           print (i)

但这给了我匹配的关键字列表。如何获取列表名称并将其添加到 pandas 列?

【问题讨论】:

  • 您现在要问多个问题。但是关于相关性 (1/3)*100 实际上是 0.3333.... 你对当前的答案不满意吗?
  • @AntonvBR 我还需要计算相关性值。因此,也可以使用 0.33。我已经尝试了很多,但我的方法似乎对我不起作用。

标签: python python-3.x pandas


【解决方案1】:

首先,我认为您应该利用从集合和字典中查找O(1) 的优势。也就是说,我将数据设置为(注意 值是集合):

d = dict(movies={'spiderman','marvels','thriller'},
         sports={'baseball','hockey','football'},
         politics={'election','china','usa'})

然后,您可以使用您的自定义逻辑transform您的系列

def f(r):
    def m(r_):
        _ = [k for (k, v) in d.items() if r_ in v]
        return _ if _ else ['Misc']
    return {item for z in [m(r_) for r_ in r] for item in z}

df.word_list.transform(f)

0    {Misc, sports, politics}
1      {Misc, sports, movies}
2              {Misc, movies}

对于 300000 行,

%timeit df.word_list.transform(f)
1.1 s ± 22.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

这不是很好但可行..

【讨论】:

  • 我有多个包含大量关键字元素的列表。那么,如何一次将其转换成字典呢?
  • 你如何定义这些列表@Mavrick?
【解决方案2】:

您可以先展平列表字典,然后通过.getmiscellaneous 查找不匹配的值,然后转换为sets 以获得唯一类别,并通过join 转换为strings:

movies=['spiderman','marvels','thriller']
sports=['baseball','hockey','football']
politics=['election','china','usa']
d = {'movies':movies, 'sports':sports, 'politics':politics}
d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}

f = lambda x: ','.join(set([d1.get(y, 'miscellaneous') for y in x]))
df['matched_list_names'] = df['word_list'].apply(f)
print (df)

                                 word_list             matched_list_names
0       [nuclear, election, usa, baseball]  politics,miscellaneous,sports
1             [football, united, thriller]    miscellaneous,sports,movies
2  [marvels, hollywood, spiderman, budget]           miscellaneous,movies

列表理解的类似解决方案:

df['matched_list_names'] = [','.join(set([d1.get(y, 'miscellaneous') for y in x])) 
                            for x in df['word_list']]

【讨论】:

  • 感谢您的评论。请检查已编辑的问题。
  • 我为我的查询添加了一个单独的问题https://stackoverflow.com/questions/51589060/add-numeric-values-beside-columns-elements-in-pandas
  • @Mavrick - 如果我的回答有帮助,请不要忘记 accept 它 - 单击答案旁边的复选标记,将其从灰色切换为已填充。谢谢。
  • 需要您的帮助。你能看看这个https://stackoverflow.com/questions/51864822/mapping-of-pandas-column-with-column-of-another-pandas-dataframe/51864988#51864988
猜你喜欢
  • 2019-02-16
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
相关资源
最近更新 更多