Python 查找与字典中的值匹配的字符串中的单词,然后在新列中返回键
我一直在尝试遍历pandas数据帧中的字符串以查找特定的单词集,在这里我成功了 然而,我意识到我不只是想找到单词,还想查看单词的语义,并将一组与我的主要关键字含义相同的单词组合在一起 我偶然发现了下面的问题,这正是我想要做的,但不幸的是,我无法让它在数据框架中工作 以下是可在链接中找到的解决方案之一:Python 查找与字典中的值匹配的字符串中的单词,然后在新列中返回键,python,pandas,string,dataframe,dictionary,Python,Pandas,String,Dataframe,Dictionary,我一直在尝试遍历pandas数据帧中的字符串以查找特定的单词集,在这里我成功了 然而,我意识到我不只是想找到单词,还想查看单词的语义,并将一组与我的主要关键字含义相同的单词组合在一起 我偶然发现了下面的问题,这正是我想要做的,但不幸的是,我无法让它在数据框架中工作 以下是可在链接中找到的解决方案之一: my_dict = {"color": ("red", "blue", "green"), "someothercolor":("orange", "blue", "white")} solut
my_dict = {"color": ("red", "blue", "green"), "someothercolor":("orange", "blue", "white")}
solutions = []
my_color = 'blue'
for key, value in my_dict.items():
if my_color in value:
solutions.append(key)
产出:
color
我的数据框:
+---+----------+--------------------------+-----------------------------+----------+--------+
| | SKU | Name | Description | Category | Colour |
+---+----------+--------------------------+-----------------------------+----------+--------+
| 0 | 7E+10 | Red Lace Midi Dress | Red Lace Midi D... | Dresses | red |
| 1 | 7E+10 | Long Armed Sweater Azure | Long Armed Sweater Azure... | Sweaters | blue |
| 2 | 2,01E+08 | High Top Ruby Sneakers | High Top Ruby Sneakers... | Shoes | red |
| 3 | 4,87E+10 | Tight Indigo Jeans | Tight Indigo Jeans... | Denim | blue |
| 4 | 2,2E+09 | T-Shirt Navy | T-Shirt Navy... | T-Shirts | blue |
+---+----------+--------------------------+-----------------------------+----------+--------+
现在我有了一个数据帧,我想在其中遍历df['Name']来找到一个值,然后我想将键添加到一个新列中。在这个例子中,它将是df['color']
+---+----------+--------------------------+-----------------------------+----------+--------+
| | SKU | Name | Description | Category | Colour |
+---+----------+--------------------------+-----------------------------+----------+--------+
| 0 | 7E+10 | Red Lace Midi Dress | Red Lace Midi D... | Dresses | |
| 1 | 7E+10 | Long Armed Sweater Azure | Long Armed Sweater Azure... | Sweaters | |
| 2 | 2,01E+08 | High Top Ruby Sneakers | High Top Ruby Sneakers... | Shoes | |
| 3 | 4,87E+10 | Tight Indigo Jeans | Tight Indigo Jeans... | Denim | |
| 4 | 2,2E+09 | T-Shirt Navy | T-Shirt Navy... | T-Shirts | |
+---+----------+--------------------------+-----------------------------+----------+--------+
预期结果:
+---+----------+--------------------------+-----------------------------+----------+--------+
| | SKU | Name | Description | Category | Colour |
+---+----------+--------------------------+-----------------------------+----------+--------+
| 0 | 7E+10 | Red Lace Midi Dress | Red Lace Midi D... | Dresses | red |
| 1 | 7E+10 | Long Armed Sweater Azure | Long Armed Sweater Azure... | Sweaters | blue |
| 2 | 2,01E+08 | High Top Ruby Sneakers | High Top Ruby Sneakers... | Shoes | red |
| 3 | 4,87E+10 | Tight Indigo Jeans | Tight Indigo Jeans... | Denim | blue |
| 4 | 2,2E+09 | T-Shirt Navy | T-Shirt Navy... | T-Shirts | blue |
+---+----------+--------------------------+-----------------------------+----------+--------+
我的代码:
colour = {'red': ('red', 'rose', 'ruby’), ‘blue’: (‘azure’, ‘indigo’, ’navy')}
def fetchColours(x):
for key, value in colour.items():
if value in x:
return key
else:
return np.nan
df['Colour'] = df['Name'].apply(fetchColours)
我得到以下错误:
TypeError: 'in <string>' requires string as left operand, not tuple
TypeError:'in'需要字符串作为左操作数,而不是元组
我不能对字符串运行元组。如何处理这个问题?您需要循环遍历字典键元组值中的每个值 根据错误消息,您无法检查
str
类型中是否存在tuple
此外,确保else
语句出现在外部for
循环之后,以便在输出默认值之前测试所有键
最后,确保检查与str.lower()
,因为字符串匹配在Python中是区分大小写的
import pandas as pd
df = pd.DataFrame({'Name': ['Red Lace Midi Dress', 'Long Armed Sweater Azure',
'High Top Ruby Sneakers', 'Tight Indigo Jeans',
'T-Shirt Navy']})
colour = {'red': ('red', 'rose', 'ruby'), 'blue': ('azure', 'indigo', 'navy')}
def fetchColours(x):
for key, values in colour.items():
for value in values:
if value in x.lower():
return key
else:
return np.nan
df['Colour'] = df['Name'].apply(fetchColours)
结果:
Name Colour
0 Red Lace Midi Dress red
1 Long Armed Sweater Azure blue
2 High Top Ruby Sneakers red
3 Tight Indigo Jeans blue
4 T-Shirt Navy blue
您正在尝试搜索字符串中的一个单词元组,而我猜您希望检查该元组中是否有任何单词在字符串中 顺便说一句,python中的字符串区分大小写 您可以替换:
if value in x:
借
非常感谢你的详细解释,它可以正常工作。假设我有多个值与df['Name']列匹配。示例:蓝色和红色蕾丝迷笛连衣裙。把这些储存在同一个电池里容易吗?(蓝色,红色)。这是可能的。我建议你自己试一试。如果你被卡住了,你可以问一个单独的问题。