【问题标题】:Check whether column value is in another column which values is a list检查列值是否在另一列中,其值为列表
【发布时间】:2019-12-11 21:42:55
【问题描述】:

我在数据框中有两列。第一个在每一行中包含一个字符串。第二个包含每行的一组字符串。我如何使用 pandas 函数检查每一行的第一列的值是否在第二列的集合中,并且是否有效?

pd.DataFrame([np.random.randint(5, size=12), np.random.randint(5, size=(12,5))]).T

如何检查第1列列表中第0列的值

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    IIUC,给定,例如:

       Col1       Col2
    0     0  [0, 1, 2]
    1     1  [2, 3, 4]
    2     2  [4, 5, 2]
    

    你可以这样做:

    df['Result'] = df.apply(lambda x: x.Col1 in x.Col2, axis = 1)
    

    输出:

       Col1       Col2  Result
    0     0  [0, 1, 2]    True
    1     1  [2, 3, 4]   False
    2     2  [4, 5, 2]    True
    

    【讨论】:

      【解决方案2】:

      使用列表理解和zip(IMO 这将比apply 更快):

      df=df.assign(Check=[a in b for a,b in zip(df[0],df[1])])
      

          0                1  Check
      0   4  [4, 4, 2, 3, 0]   True
      1   4  [1, 0, 2, 1, 4]   True
      2   0  [2, 1, 1, 2, 2]  False
      3   0  [0, 3, 3, 2, 3]   True
      4   4  [3, 0, 0, 3, 1]  False
      5   1  [0, 2, 0, 3, 4]  False
      6   0  [4, 3, 4, 1, 1]  False
      7   1  [2, 0, 0, 3, 1]   True
      8   2  [3, 3, 3, 2, 4]   True
      9   2  [3, 0, 0, 4, 1]  False
      10  0  [3, 3, 3, 4, 3]  False
      11  1  [0, 3, 3, 2, 1]   True
      

      在测试数据上的表现

      【讨论】:

      • 哇,不错! :)
      • BTW 想分享数据吗?
      • @WeNYoBen 哪个?可测试的片段? df=pd.DataFrame([np.random.randint(5, size=12), np.random.randint(5, size=(12,5))]).T
      • @anky_91 谢谢 :-) 你想添加 isin 的时间吗?
      • @anky_91:不错。我从没想过 listcomp 比 pandas df 构建和 isin 快那么多! :) +1
      【解决方案3】:

      IIUCisin

      pd.DataFrame(df[1].values.tolist(),index=df.index).isin(df[0]).any(1)
      

      【讨论】:

        【解决方案4】:

        使用 numpy 广播和any

        示例为:

        df:    
        Out[429]:
            0                1
        0   1  [0, 2, 2, 2, 0]
        1   0  [0, 4, 3, 2, 4]
        2   4  [4, 1, 0, 3, 2]
        3   4  [1, 0, 1, 4, 1]
        4   0  [3, 3, 1, 2, 2]
        5   4  [0, 4, 2, 2, 0]
        6   1  [2, 1, 1, 1, 0]
        7   4  [0, 4, 2, 4, 0]
        8   0  [4, 4, 4, 4, 4]
        9   0  [4, 2, 3, 3, 1]
        10  2  [0, 4, 2, 3, 2]
        11  3  [1, 3, 2, 2, 1]
        
        df['Flag_isin'] = (df[0].values[:, None] == np.vstack(df[1].values)).any(1)
        
        Out[431]:
            0                1  Flag_isin
        0   1  [0, 2, 2, 2, 0]      False
        1   0  [0, 4, 3, 2, 4]       True
        2   4  [4, 1, 0, 3, 2]       True
        3   4  [1, 0, 1, 4, 1]       True
        4   0  [3, 3, 1, 2, 2]      False
        5   4  [0, 4, 2, 2, 0]       True
        6   1  [2, 1, 1, 1, 0]       True
        7   4  [0, 4, 2, 4, 0]       True
        8   0  [4, 4, 4, 4, 4]      False
        9   0  [4, 2, 3, 3, 1]      False
        10  2  [0, 4, 2, 3, 2]       True
        11  3  [1, 3, 2, 2, 1]       True
        

        【讨论】:

          猜你喜欢
          • 2022-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-30
          • 1970-01-01
          相关资源
          最近更新 更多