我的dataframe中有三列文本,我想将相同的function应用到。以下是我在下面尝试过的内容。我应该将什么作为参数传递给我的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)  

我不确定如何分别编写每个列中的function并将function应用于它。有任何想法吗?

分析解答

将function重写为

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

并仅使用申请:

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