重新命名類別
可領養狗狗資料集的 likes_children 欄位需要更新。以下是目前的次數統計:
Maybe? 1718
yes 1172
no 47
有兩點很明顯:大小寫不一致,以及 Maybe? 類別中出現了 ?。資料應該要更乾淨,你的任務是做一些調整。
本練習屬於課程
在 Python 中處理類別資料
練習說明
- 建立名為
my_changes的字典,將Maybe?類別更新為Maybe。 - 使用
my_changes字典重新命名likes_children的類別。 - 再次更新類別,使「所有」類別都變成大寫,使用
.upper()方法。 - 列印更新後
likes_childrenSeries 的各個類別。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the my_changes dictionary
____
# Rename the categories listed in the my_changes dictionary
dogs["likes_children"] = ____
# Use a lambda function to convert all categories to uppercase using upper()
dogs["likes_children"] = dogs["likes_children"].____.____(lambda c: c.upper())
# Print the list of categories
print(____)