'Use a list with function names to iteratively apply over a dataframe column

Context: I'm allowing a user to add specific methods for a cleaning process pipeline (appended to a main list with all the methods chosen). Each element from this list is the name of a function.

My quesiton is:

Why does this work:

dataframe[cleanedCol] =dataframe[colToClean].apply(replace_contractions).apply(remove_links).apply(remove_emails)

But something like this doesn't?

pipeline = ['replace_contractions','remove_links','remove_emails']
for method in pipeline:
     dataframe[cleanedColumn] = dataframe[columnToClean].apply(method)

How could I iteratively apply each one of the methods from the list (by the order they are in the list) to the dataframe column?

Thank you in advance!



Solution 1:[1]

You would either have to convert those strings to actual function objects or even better just store the function objects instead of the names as strings

pipeline = [replace_contractions, remove_links, remove_emails]
for method in pipeline:
     dataframe[cleanedColumn] = dataframe[columnToClean].apply(method)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Cory Kramer