【问题标题】:Need to split and check if the list element is available in pandas df需要拆分并检查列表元素是否在 pandas df 中可用
【发布时间】: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 在同一个列表中怎么办?

标签: python pandas list split


【解决方案1】:

这是我的建议:

#get all combinations of 2 from list1 &list2 both ways:
l = [(i,k) for i in list1 for k in list2] + [(i,k) for i in list2 for k in list1]

#get the items from positions 0 and 2 as tuple:
df['new']=df.Camp.apply(lambda x: (x.split('_')[0],x.split('_')[2]))

#check if this pair is in l:
df['check']=df.new.apply(lambda x: x in l)

del df['new']

>>>print(df)

   ind               Camp  check
0    1  Ball_Dog_Wild_Ear  False
1    2   Dog_Ball_Pet_Dos   True
2    3   Wild_Dos_Cat_Pre   True
3    4   Pet_Cer_Crow_Tre   True
4    5   Dos_Cat_Wild_Tre  False

【讨论】:

    【解决方案2】:

    使用列表理解:

    cond = lambda x, y: (x in list1 and y in list2) or (x in list2 and y in list1)
    df["res"] = [cond(zeroth, second) for zeroth, _, second, _ in df.Camp.str.split("_")]
    

    cond 以交叉方式检查条目是否在相应的列表中。

    >>> df
    
       ind               Camp    res
    0    1  Ball_Dog_Wild_Ear  False
    1    2   Dog_Ball_Pet_Dos   True
    2    3   Wild_Dos_Cat_Pre   True
    3    4   Pet_Cer_Crow_Tre   True
    4    5   Dos_Cat_Wild_Tre  False
    

    【讨论】:

      【解决方案3】:

      您可以使用itertools.productsetisin 来做到这一点:

      from itertools import product
      
      #create a set list of products from list1 and list2
      sl = [set(i) for i in product(list1, list2)]
      
      #split, slice for 0 and 2, apply set and check using isin set list(sl)
      df['check'] = (df['Camp'].str.split('_', expand=True)
                               .loc[:,[0,2]].apply(set, axis=1).isin(sl))
      
      df
      

      输出:

         ind               Camp  check
      0    1  Ball_Dog_Wild_Ear  False
      1    2   Dog_Ball_Pet_Dos   True
      2    3   Wild_Dos_Cat_Pre   True
      3    4   Pet_Cer_Crow_Tre   True
      4    5   Dos_Cat_Wild_Tre  False
      

      【讨论】:

        猜你喜欢
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        相关资源
        最近更新 更多