简体   繁体   中英

How to convert to lowercase all columns except a few specific ones?

I would like to convert to lowercase all columns within a dataframe except two. To convert all the dataframe I usually do

df=df.apply(lambda x: x.astype(str).str.lower())

My dataset is

Time    Name       Surname       Age  Notes        Comments
 12     Mirabel    Gutierrez     23   None        Already Paid
 09     Kim        Stuart        45   In debt     Should refund 100 EUR

and so on.

I would like to transform into lowercase all the columns except Notes and Comments .

Time    Name       Surname       Age  Notes        Comments
 12     mirabel    gutierrez     23   None        Already Paid
 09     kim        stuart        45   In debt     Should refund 100 EUR

What can I try?

You probably simply want to create a list of the relevant columns:

lowerify_cols = [col for col in df if col not in ['Notes','Comments']]

Then you can use your code snippet:

df[lowerify_cols]= df[lowerify_cols].apply(lambda x: x.astype(str).str.lower(),axis=1)

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