1

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))

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hm, small problem. I wanted to return x so I can return the dataframe with the same size. how would i do that? I'm noticing that if I incorporate my "else: return x" in there, the items don't change. actually i think i solved it by using 'break'
Hm, actually using 'break' creates a problem because it doesn't go through the entire list... any thoughts?
@user3314418 I'm not sure I follow: why doesn't this solution work? It may help to give an input and expected output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.