首页 > 解决方案 > 在 Python 中使用字典的值用字典的键标记列

问题描述

data = {'Name':['Tom', 'nick', 'krish', 'jack'],
        'Note':['The color is red', 'This is a white blouse', 'I love the blue hoodie', 'What is that orange box?']}
  
# Create DataFrame
df = pd.DataFrame(data)

这是我的数据集 DF。我想在“Note”列中找到一些颜色词,所以我创建了一个字典,所有值都是我要搜索的颜色词。然后我想创建一个返回字典键的新列。

F={'One':'red','Two':'white', 'Three':'blue', 'Four':'orange'}

我想在 for 循环中执行此操作,但它不起作用。似乎 y 参数在被赋值后不再被替换。有人可以建议吗?谢谢!

for i in range(4):
    print(list(F.values())[i])
    df['C']=np.where(df['Note'].str.contains(list(F.values())[i]), list(F.keys())[i], 
                          )

标签: pythondictionaryfor-loop

解决方案


如果我理解正确,您需要:

df['Match'] = df['Note'].replace({f'.*{v}.*': k for k, v in F.items()}, regex=True)

现在:

>>> df
    Name                      Note  Match
0    Tom          The color is red    One
1   nick    This is a white blouse    Two
2  krish    I love the blue hoodie  Three
3   jack  What is that orange box?   Four
>>> 

或者,如果您只想替换以下单词:

df['Match'] = df['Note'].replace({v: k for k, v in F.items()}, regex=True)

输出:

>>> df
    Name                      Note                    Match
0    Tom          The color is red         The color is One
1   nick    This is a white blouse     This is a Two blouse
2  krish    I love the blue hoodie  I love the Three hoodie
3   jack  What is that orange box?   What is that Four box?
>>>

推荐阅读