'apply function to each row

I have a function which splits text under a certain criteria:

s = master_df.iloc[0,6]

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

print(find_between( s, "Presentation", "Questions and Answers" ))

I want to apply this function to each row of my data frame. I am struggling with s argument which prevents populating. I tried with a loop and modified function as follows:


def find_between( df, s, first, last ):
    for i in range(0, 2000):
        s = df.iloc[i,6] # 6th column
        try:
                start = s.index( first ) + len( first )
                end = s.index( last, start )
                return s[start:end]
        except ValueError:
                return ""

# print(find_between( s, "Presentation", "Questions and Answers" ))

master_df['Presentation'] = find_between( master_df, s, "Presentation", "Questions and Answers" )

But received the column with the same value in each cell, iteration did not work. Can you please help me what change is required in the code, so I split text of each cell and get this splitted text into a new column?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source