การใช้ชนิดข้อมูลที่เหมาะสม

การวิเคราะห์กิจกรรมตำรวจด้วย pandas

Kevin Markham

Founder, Data School

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

ri.dtypes
stop_date             object
stop_time             object
driver_gender         object
...                      ...
stop_duration         object
drugs_related_stop      bool
district              object
  • object: Python strings (หรือออบเจกต์ Python อื่น ๆ)
  • bool: ค่า True และ False
  • ชนิดอื่น ๆ: int, float, datetime, category
การวิเคราะห์กิจกรรมตำรวจด้วย pandas

ชนิดข้อมูลสำคัญอย่างไร?

  • ส่งผลต่อการดำเนินการที่ทำได้
  • หลีกเลี่ยงการเก็บข้อมูลเป็น string (เมื่อเป็นไปได้)
    • int, float: รองรับการดำเนินการทางคณิตศาสตร์
    • datetime: รองรับ attribute และเมธอดด้านวันที่
    • category: ใช้หน่วยความจำน้อยและทำงานเร็วกว่า
    • bool: รองรับการดำเนินการเชิงตรรกะและคณิตศาสตร์
การวิเคราะห์กิจกรรมตำรวจด้วย pandas

แก้ไขชนิดข้อมูล

apple
      date   time   price
0  2/13/18  16:00  164.34
1  2/14/18  16:00  167.37
2  2/15/18  16:00  172.99
apple.price.dtype
dtype('O')
apple['price'] = 
  apple.price.astype('float')
apple.price.dtype
dtype('float64')
  • Dot notation: apple.price
  • Bracket notation: apple['price']
การวิเคราะห์กิจกรรมตำรวจด้วย pandas

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

การวิเคราะห์กิจกรรมตำรวจด้วย pandas

Preparing Video For Download...