简体   繁体   中英

applying a function to all columns in pandas

This is part of a data frame:

df2:
**headache**                                             **Sweating**                        
C0018681 / Headache / Sign or Symptom        C0038990 / Sweating / Finding
C0233408 / Disorientated in time / Finding   C0037195 / Sinus headache / Finding

I am going to remove spaces in all columns using the following function:

def codeCleaning (df, column):
    df[column].replace('\s*/\s*', '/', regex=True, inplace = True)
    df[column] = df[column].str.strip() 
    df[column] =df[column].str.lower()
    return df

I created a list of the columns header as follow

column=list(df2.columns.values)

Then I tried to apply the function to all columns, but it did not work. This is my code:

df2 = codeCleaning (df2,column )

How I can solve it?

df.applymap(lambda x: '/'.join(map(str.lower, map(str.strip, x.split('/')))))

                                 headache                         Sweating
0       c0018681/headache/sign or symptom        c0038990/sweating/finding
1  c0233408/disorientated in time/finding  c0037195/sinus headache/finding

To address mixed types, you can cast as str

df.astype(str).applymap(lambda x: '/'.join(map(str.lower, map(str.strip, x.split('/')))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM