【问题标题】:check if an item in a list is available in a column which is of type list检查列表中的项目是否在列表类型的列中可用
【发布时间】:2020-05-18 19:40:57
【问题描述】:

我正在尝试使用数据框中的一列迭代我拥有的列表,该列在每一行中都有列表。

list1 = ['正在安装','安装','已安装','替换','修复','修复','替换','零件','使用','新']

df[lwr_nopunc_spc_nostpwrd].head(3)

['daily', 'ask', 'questions']
['daily', 'system', 'check', 'task',  'replace']
['inspection', 'complete', 'replaced', 'horizontal', 'sealing', 'blade', 'inspection', 'complete', 'issues', 'found']

现在,我想在我的数据框中获取两个新列,如果 <new column one> df[lwr_nopunc_spc_nostpwrd] 行中的任何一项存在于 list1 <new columns two> 中,如果 list1 中的所有项目为一个,则它们应该显示 true 或 false存在于 df[lwr_nopunc_spc_nostpwrd] 行中

请告诉我如何实现。我尝试了all()any() 方法,但似乎不起作用。

def prt_usd(row):
    return(any(item in query['lwr_nopunc_spc_nostpwrd'] for item in part))

for row in query['lwr_nopunc_spc_nostpwrd']:
    prt_usd(query['lwr_nopunc_spc_nostpwrd'])

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    你可以做到 applyset 喜欢:

    # I changed the list to the second row to show that the column all works
    list1 = ['daily', 'system', 'check', 'task',  'replace']
    # create a set from it
    s1 = set(list1)
    
    # for any word, check that the intersection of s1 
    # and the set of the list in this row is not empty
    df['col_any'] = df['lwr_nopunc_spc_nostpwrd'].apply(lambda x: any(set(x)&s1))
    
    # for all, subtract the set of this row from the set s1, 
    # if not empty then it return True with any
    # that you reverse using ~ in front of it to get True if all words from s1 are in this row
    df['col_all'] = ~df['lwr_nopunc_spc_nostpwrd'].apply(lambda x: any(s1-set(x)))
    print (df)
                                 lwr_nopunc_spc_nostpwrd  col_any  col_all
    0                            [daily, ask, questions]     True    False
    1              [daily, system, check, task, replace]     True     True
    2  [inspection, complete, replaced, horizontal, s...    False    False
    

    【讨论】:

      【解决方案2】:

      您可以将一些集合算术与列表推导一起使用,如下所示(请注意,我简化了您的示例以具有更明显的测试用例):

      import pandas as pd
      
      list1 = ['installing', 'replace']
      set1 = set(list1)
      
      df = pd.DataFrame({'col1': [['daily', 'ask'], 
                                  ['daily', 'replace'],
                                  ['installing', 'replace', 'blade']]})
      
      # new1 should be True when the intersection of list1 with the row from col1 is not empty
      df['new1'] = [set1.intersection(set(row)) != set() for row in df.col1]
      
      # new2 should be True when list1 is a subset of the row from col1 
      df['new2'] = [set1.issubset(set(row)) for row in df.col1]
      
      df
      
          col1                          new1   new2
      0   [daily, ask]                  False  False
      1   [daily, replace]              True   False
      2   [installing, replace, blade]  True   True
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 1970-01-01
        • 2011-09-02
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 2022-08-24
        相关资源
        最近更新 更多