【发布时间】:2020-09-19 11:20:23
【问题描述】:
假设我的数据集
name what
A apple[red]
B cucumber[green]
C dog
C orange
D banana
D monkey
E cat
F carrot
.
.
我想创建并指定一个列表,如果该列包含该列表中包含的值,我想将指定的值设为新列。
列表值
fruit = ['apple', 'banana', 'orange']
animal = ['dog', 'monkey', 'cat']
vegetable = ['cucumber', 'carrot']
得到我想要的结果
name what class
A apple fruit
B cucumber vegetable
C dog animal
C orange fruit
D banana fruit
D monkey animal
E cat animal
F carrot vegetable
列表值和列值不“匹配”,必须包含。
感谢您的阅读。
【问题讨论】:
-
到目前为止你尝试了什么?
-
@Anwarvic df1 = df['column anme'].str.contains("|".join(listname)) ,, 不能指定多个列表,也说不出话我指定了。
标签: python pandas dataframe contains