Python 使用“应用于多个条件”的自定义方法

Python 使用“应用于多个条件”的自定义方法,python,pandas,dataframe,apply,Python,Pandas,Dataframe,Apply,我试图根据一些条件更改一列的值,这些条件在下面的函数中使用apply方法进行了详细说明,但是我无法获得下面的预期输出 lst = [np.nan,["google_newyork","google_NewYork","google_LA"],["google_newyork","google_LA"],["google_newyork","google_newyork

我试图根据一些条件更改一列的值,这些条件在下面的函数中使用apply方法进行了详细说明,但是我无法获得下面的预期输出

lst = [np.nan,["google_newyork","google_NewYork","google_LA"],["google_newyork","google_LA"],["google_newyork","google_newyork"]]
  
df = pd.DataFrame(lst, columns =['subcompanies_matches']) 


然而我得到了这个错误,级数的真值是模糊的。因此,我想知道对于此类问题,apply是否是合适的函数?

试试这个,您不需要数据帧:

def check_subcompanies(subcompanies_concat):
    """ Returns True if subcompanies match else False """
    if  subcompanies_concat != None: 
        subcompany1 = subcompanies_concat[0].lower()
        subcompany2 = [x.lower() for x in subcompanies_concat[1:]]
        if subcompany1 in subcompany2:
            return True
        else:
            return False
    else subcompanies_concat == None:
        return True
df = df.apply(check_subcompanies)
输出:

lst = [["google_newyork","google_newyork"], None, ["google_newyork","google_NewYork","google_LA"], ["google_newyork","google_LA"]]

def check_subcompanies(subcompanies_concat):
    """ Returns True if subcompanies match else False """
    if  subcompanies_concat: 
        subcompany1 = subcompanies_concat[0].lower()
        subcompany2 = [x.lower() for x in subcompanies_concat[1:]]
        if subcompany1 in subcompany2:
            return True
        else:
            return False
    elif(subcompanies_concat == None):
        return True


list(map(check_subcompanies, lst))

dataframe的定义不正确,它返回错误,请用更清晰的方法显示您的df,以及您想从check_Subcompanies中得到什么。对不起,现在我做了相应的更正。请尝试回答,您定义的dataframe对LST没有任何好处谢谢您的回答,然而,结果有时会改变,[真,真,真,假]
lst = [["google_newyork","google_newyork"], None, ["google_newyork","google_NewYork","google_LA"], ["google_newyork","google_LA"]]

def check_subcompanies(subcompanies_concat):
    """ Returns True if subcompanies match else False """
    if  subcompanies_concat: 
        subcompany1 = subcompanies_concat[0].lower()
        subcompany2 = [x.lower() for x in subcompanies_concat[1:]]
        if subcompany1 in subcompany2:
            return True
        else:
            return False
    elif(subcompanies_concat == None):
        return True


list(map(check_subcompanies, lst))
[True, True, True, False]