gpt4 book ai didi

python - 列表元素与 pandas 列的关键字匹配

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:28 26 4
gpt4 key购买 nike

我有如下元素列表:

 A=  ['loans','s-class','veyron','trump','rihana','drake','election']

我还有另一个 pandas 数据框 B,其中包含列 categorywords,它是逗号分隔的字符串:-

category              words
audi a4, a6
bugatti veyron, chiron
mercedez s-class, e-class
dslr canon, nikon
apple iphone,macbook,ipod
finance sales,loans,sales price
politics trump, election, votes
entertainment spiderman,thor, ironmen
music beiber, rihana,drake
........ ..............
......... .........

我只想将列表 A 的元素映射到列 words 并将相应的 category 分配到一个新列表中。因此,预期输出将是。

matched_categories=['finance','mercedez','bugatti','politics','music','music','politics']

最佳答案

boolean indexing 过滤与 iat选择第一个匹配值:

#if always matched all values
matched_categories = [df.loc[df['words'].str.contains(x), 'category'].iat[0] for x in A]
print (matched_categories)
['finance', 'mercedez', 'bugatti', 'politics', 'music', 'music', 'politics']

如果某些值不匹配,则更通用的解决方案 - 然后返回 不匹配 值:

#added last aaa value
A = ['loans','s-class','veyron','trump','rihana','drake','election','aaa']

matched_categories = [next(iter(df.loc[df['words'].str.contains(x),'category']),'not matched')
for x in A]
print (matched_categories)
['finance', 'mercedez', 'bugatti', 'politics', 'music', 'music', 'politics', 'not matched']

关于python - 列表元素与 pandas 列的关键字匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com