简体   繁体   中英

using a for loop within a .apply() for pandas dataframes

I'd like to remove all items within a list. I've tried the below code to iterate through all items in a list within the context of a pandas .apply(). However, the function remove(x) only seems to be going to the first item in remove_l. How can I make sure it goes through all items in remove_l?

I know I can just create separate if statements, which I've already done, but I'd like to implement using a for loop just in case the list gets longer.

remove_l = [r'[A-Za-z]+(?:\/)', r'Today, ', '-']

def remove(x):
    for phrase in remove_l:
        if re.search(phrase, x):
            if phrase == '-':
                new = x.replace(phrase, ' ')
            else: 
                new = x[re.search(phrase, x).span()[1]:].strip()
            return new 
        else: 
            return x


#check up on items 
#60, 330, 347, 411, 647
#idx = nocountries_df[nocountries_df.Name.str.contains('\/')].Name.index
nocountries_df.Name.apply(lambda x: remove(x))

This is an indentation problem, when it hits the first return (in the for loop) it returns that value:

def remove(x):
    for phrase in remove_l:
        if re.search(phrase, x):
            if phrase == '-':
                new = x.replace(phrase, ' ')
            else: 
                new = x[re.search(phrase, x).span()[1]:].strip()
            return new  # <- returns here (in first phase) 
        else: 
            return x  # <- or returns here (in first phase)

You want to return after the for loop, it's probably easiest just to change x in the for loop:

def remove(x):
    for phrase in remove_l:
        if re.search(phrase, x):
            if phrase == '-':
                x = x.replace(phrase, ' ')
            else: 
                x = x[re.search(phrase, x).span()[1]:].strip()
    return x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM