[英]How to convert to lowercase all columns except a few specific ones?
我想將 dataframe 中的所有列轉換為小寫,除了兩個。 轉換所有 dataframe 我通常做
df=df.apply(lambda x: x.astype(str).str.lower())
我的數據集是
Time Name Surname Age Notes Comments
12 Mirabel Gutierrez 23 None Already Paid
09 Kim Stuart 45 In debt Should refund 100 EUR
等等。
我想將除Notes
和Comments
之外的所有列都轉換為小寫。
Time Name Surname Age Notes Comments
12 mirabel gutierrez 23 None Already Paid
09 kim stuart 45 In debt Should refund 100 EUR
我可以嘗試什么?
您可能只是想創建相關列的列表:
lowerify_cols = [col for col in df if col not in ['Notes','Comments']]
然后你可以使用你的代碼片段:
df[lowerify_cols]= df[lowerify_cols].apply(lambda x: x.astype(str).str.lower(),axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.