pandas - Pandas 使用字典替换值和值列表
问题描述
我有以下字典和系列。
product_type_dic = {'Facial Care':['Facial_cleanser', 'Beard_oil'], 'Eye Care':['Around-eye_cream', 'Eyeliner']}
s = pd.Series(['Facial_cleanser', 'Beard_oil', 'Around-eye_cream', 'Eyeliner'])
我的目标是使用 Series 获取字典的键值。所以结果将是
'Facial Care'
'Facial Care'
'Eye Care'
'Eye Care'
谢谢
解决方案
Series.map
与逆product_type_dic
字典一起使用:
product_type_dic = {
"Facial Care": ["Facial_cleanser", "Beard_oil"],
"Eye Care": ["Around-eye_cream", "Eyeliner"],
}
inv_product_type_dic = {vv: k for k, v in product_type_dic.items() for vv in v}
s = pd.Series(["Facial_cleanser", "Beard_oil", "Around-eye_cream", "Eyeliner"])
print(s.map(inv_product_type_dic))
印刷:
0 Facial Care
1 Facial Care
2 Eye Care
3 Eye Care
dtype: object
推荐阅读
- c# - 错误:ToastNotification 类型在 Windows.Foundation.UniversalApiContract 和 Windows.UI 中都存在
- c# - 在 Unity 中使用 AdMob 实现插页式广告
- python - 爆炸列
- linux - 使用 sed 查找匹配两个日期的一系列行
- c++ - Postgres自定义类型映射到C结构?
- flutter - 有没有办法在 Flutter 上找到连接速度?
- laravel - 我无法显示来自我的控制器的一些信息,我必须在 @section('content') @endsection 之间显示的信息
- php - MySQL PDO 多个 UPDATE
- java - 与 log4j2 集成后,日志未反映在报告门户中
- mysql - 需要进行哪些更改来防止警告:#1287 在我的查询中不推荐在表达式中设置用户变量?