本文介绍了在Pandas Dataframe&中查找多个字典键.返回多个匹配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的格式已关闭,那么第一次发布会提前道歉.

First time posting so apologies in advance if my formatting is off.

这是我的问题:

我创建了一个Pandas数据框,其中包含多行文本:

I've created a Pandas dataframe which contains multiple rows of text:

d = {'keywords' :['cheap shoes', 'luxury shoes', 'cheap hiking shoes']}
keywords = pd.DataFrame(d,columns=['keywords'])
In [7]: keywords
Out[7]:
        keywords
0  cheap shoes
1  luxury shoes
2  cheap hiking shoes

现在我有一个包含以下键/值的字典:

Now I have a dictionary that contains the following keys / values:

labels = {'cheap' : 'budget', 'luxury' : 'expensive', 'hiking' : 'sport'}

我想做的是找出数据框中是否存在字典中的键,如果存在,则返回适当的值

What I would like to do is find out whether a key in the dictionary exist in the dataframe, and if so, return the appropriate value

我可以使用以下方法到达那里:

I was able to somewhat get there using the following:

for k,v in labels.items():
   keywords['Labels'] = np.where(keywords['keywords'].str.contains(k),v,'No Match')

但是,输出缺少前两个键,仅捕获了最后一个远足"键

However, the output is missing the first two keys and is only catching the last "hiking" key

    keywords            Labels
0   cheap shoes         No Match
1   luxury shoes        No Match
2   cheap hiking shoes  sport

此外,我还想知道是否存在一种方法来捕获字典中由|分隔的多个值. ,因此理想的输出应如下所示:

Additionally, I'd also like to know if there's a way to catch multiple values in the dictionary separated by | , so the ideal output would look like this

    keywords            Labels
0   cheap shoes         budget
1   luxury shoes        expensive
2   cheap hiking shoes  budget | sport

非常感谢您的帮助或指导.

Any help or guidance is much appreciated.

欢呼

推荐答案

当然有可能.这是一种方法.

It's certainly possible. Here is one way.

d = {'keywords': ['cheap shoes', 'luxury shoes', 'cheap hiking shoes', 'nothing']}

keywords = pd.DataFrame(d,columns=['keywords'])

labels = {'cheap': 'budget', 'luxury': 'expensive', 'hiking': 'sport'}

df = pd.DataFrame(d)

def matcher(k):
    x = (i for i in labels if i in k)
    return ' | '.join(map(labels.get, x))

df['values'] = df['keywords'].map(matcher)

#              keywords          values
# 0         cheap shoes          budget
# 1        luxury shoes       expensive
# 2  cheap hiking shoes  budget | sport
# 3             nothing

这篇关于在Pandas Dataframe&中查找多个字典键.返回多个匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:27