Home > Software engineering >  Use a list with function names to iteratively apply over a dataframe column
Use a list with function names to iteratively apply over a dataframe column

Time:05-14

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!

CodePudding user response:

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