【发布时间】:2021-09-29 16:46:00
【问题描述】:
我需要从两个列表中获取可用的项目,然后检查它们是否在字符串中可用。
list1 = ['Cat', 'Dog', 'Crow']
list2 = ['Wild', 'Pet']
Df
ind Camp
1 Ball_Dog_Wild_Ear
2 Dog_Ball_Pet_Dos
3 Wild_Dos_Cat_Pre
4 Pet_Cer_Crow_Tre
5 Dos_Cat_Wild_Tre
我需要根据“_”拆分“Camp”,然后检查位置 0 和 2,如果列表中的项目完全匹配。
期望的输出:
ind Camp
1 Ball_Dog_Wild_Ear False #False because 0 position has ball(not in list)
2 Dog_Ball_Pet_Dos True #True because 0 and 2 both available in the list
3 Wild_Dos_Cat_Pre True
4 Pet_Cer_Crow_Tre True
5 Dos_Cat_Wild_Tre False
我可以通过df['Camp'].str.rsplit("_", expand=True)进行拆分
并与.isin() 核对,但不确定我们如何为每一行执行以获得所需的输出。
【问题讨论】:
-
什么是list1和list2?有list3、list4等吗?
-
不,只有 2 个列表
-
那为什么要保留 2 个列表?
-
检查位置 0 和 2
-
如果位置 0 和 2 在同一个列表中怎么办?