Search code examples
pythonpandaspython-applymap

Apply Function for Text Cleaning to Multiple Columns


I have three columns of text in my dataframe that I want to apply the same function to. Here is what I have tried below. What should I pass as a parameter to my function?

def clean_columns():
     df['column'] = df['column'].str.replace('[^\w\s]',' ')\
                  .str.replace('hello',' ')\
                  .str.replace('goodbye',' ')\
                  .str.lower()\
                  .str.split()
df[['Col1', 'Col2', 'Col3']].applymap(clean_columns)  

I am not sure how to write the function in a way were it takes in each column separately and applies the function to it. Any ideas?


Solution

  • Rewrite the function as

    def clean_columns(col):
        return col.str.replace('[^\w\s]',' ')\
                      .str.replace('hello',' ')\
                      .str.replace('goodbye',' ')\
                      .str.lower()\
                      .str.split()
    

    and use apply only:

    df[['col1', 'col2', 'col3']] = df[['col1', 'col2', 'col3']].apply(clean_column)