python - 在 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],
)
解决方案
如果我理解正确,您需要:
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?
>>>
推荐阅读
- firebase - Flutter _scrollController.addListener 从不执行
- html - 如何查看多页源代码?
- javascript - 使用 ComboBox 填充文本框
- css - 更改角度材质单选按钮上的 CSS(例如 bgcolor)
- google-apps-script - 某些用户收到“找不到具有给定 ID 的项目,或者您无权访问它”错误
- bash - 根据bash中的匹配设置变量等于值
- python - 查找源代码在 python 中不起作用
- flutter - 颤振将价值添加到列表中
- python - 调整图像大小时自动缩放
- arrays - 为什么这不会产生嵌套数组?