การอัปเดตหมวดหมู่

การทำงานกับข้อมูลเชิงกลุ่มใน Python

Kasey Jones

Research Data Scientist

ตัวแปร breed

จำนวนนับของสายพันธุ์:

dogs["breed"] = dogs["breed"].astype("category")
dogs["breed"].value_counts()
Unknown Mix                 1524
German Shepherd Dog Mix     190
Dachshund Mix               147
Labrador Retriever Mix      83
Staffordshire Terrier Mix   62
...
การทำงานกับข้อมูลเชิงกลุ่มใน Python

การเปลี่ยนชื่อหมวดหมู่

เมธอด rename_categories:

Series.cat.rename_categories(new_categories=dict)

สร้าง dictionary:

my_changes = {"Unknown Mix": "Unknown"}

เปลี่ยนชื่อหมวดหมู่:

dogs["breed"] = dogs["breed"].cat.rename_categories(my_changes)
การทำงานกับข้อมูลเชิงกลุ่มใน Python

ตัวแปร breed หลังอัปเดต

จำนวนนับของสายพันธุ์:

dogs["breed"].value_counts()
Unknown                     1524
German Shepherd Dog Mix     190
Dachshund Mix               147
Labrador Retriever Mix      83
Staffordshire Terrier Mix   62
...

เปลี่ยนหลายรายการพร้อมกัน:

my_changes = {
  old_name1: new_name1,
  old_name2: new_name2,
  ...
}
Series.cat.rename_categories(
  my_changes
)
การทำงานกับข้อมูลเชิงกลุ่มใน Python

การเปลี่ยนชื่อหมวดหมู่ด้วยฟังก์ชัน

อัปเดตหลายหมวดหมู่พร้อมกัน:

dogs['sex'] = dogs['sex'].cat.rename_categories(lambda c: c.title())

dogs['sex'].cat.categories
Index(['Female', 'Male'], dtype='object')
การทำงานกับข้อมูลเชิงกลุ่มใน Python

ปัญหาที่พบบ่อยในการแทนที่ค่า

  • ต้องใช้ชื่อหมวดหมู่ใหม่
# Does not work! "Unknown" already exists
use_new_categories = {"Unknown Mix": "Unknown"}
  • ไม่สามารถรวมสองหมวดหมู่เป็นหนึ่งได้
# Does not work! New names must be unique
cannot_repeat_categories = {
    "Unknown Mix": "Unknown",
    "Mixed Breed": "Unknown"
}
การทำงานกับข้อมูลเชิงกลุ่มใน Python

การเตรียมข้อมูลสำหรับการรวมหมวดหมู่

สีของสุนัข:

dogs["color"] = dogs["color"].astype("category")
print(dogs["color"].cat.categories)
Index(['apricot', 'black', 'black and brown', 'black and tan',
       'black and white', 'brown', 'brown and white', 'dotted', 'golden',
       'gray', 'gray and black', 'gray and white', 'red', 'red and white',
       'sable', 'saddle back', 'spotty', 'striped', 'tricolor', 'white',
       'wild boar', 'yellow', 'yellow-brown'],
      dtype='object')
...
การทำงานกับข้อมูลเชิงกลุ่มใน Python

ตัวอย่างการรวมหมวดหมู่

สร้าง dictionary แล้วใช้ .replace:

update_colors = {
    "black and brown": "black",
    "black and tan": "black",
    "black and white": "black",
}
dogs["main_color"] = dogs["color"].replace(update_colors)

ตรวจสอบชนิดข้อมูลของ Series:

dogs["main_color"].dtype
dtype('O')
การทำงานกับข้อมูลเชิงกลุ่มใน Python

แปลงกลับเป็น categorical

dogs["main_color"] = dogs["main_color"].astype("category")
dogs["main_color"].cat.categories
Index(['apricot', 'black', 'brown', 'brown and white', 'dotted', 'golden',
       'gray', 'gray and black', 'gray and white', 'red', 'red and white',
       'sable', 'saddle back', 'spotty', 'striped', 'tricolor', 'white',
       'wild boar', 'yellow', 'yellow-brown'],
      dtype='object')
การทำงานกับข้อมูลเชิงกลุ่มใน Python

มาฝึกกันเถอะ!

การทำงานกับข้อมูลเชิงกลุ่มใน Python

Preparing Video For Download...