【发布时间】:2021-04-19 19:26:30
【问题描述】:
我有一个类似这样的字典:
dict = {'color':['red', 'blue', 'green'], 'fruits':['apple', 'banana', 'grape'], 'animal':['cat', 'dog']}
和 df 有两列;具有多个字符串的文本列:
index | text
-------------------------------
a | house, chair, green
-------------------------------
b | yellow, banana, wall
--------------------------------
c | dog, brown, grass
--------------------------------
如果文本列中的任何字符串与 dict.values 匹配,我想使用来自 dict 的键对向 df 添加额外的列,因此对于 a - color / b - fruits / c - animal。
我尝试使用isin 获取列表,但认为使用 dict 可能会更有效。?任何帮助表示赞赏
【问题讨论】:
-
欢迎来到 Stack Overflow!请拨打tour,阅读what's on-topic here、How to Ask和question checklist,并提供minimal reproducible example。 “为我实现此功能”与此站点无关。你必须诚实地尝试,然后就你的算法或技术提出一个具体问题。
标签: python string dictionary isin