การตรวจสอบความถูกต้องของข้อมูล

การรวมข้อมูลด้วย pandas

Aaren Stubberfield

Instructor

ตรวจสอบข้อมูลของเรา

ปัญหาที่อาจเกิดจากการ merge:

DataFrame สอง ตัววางต่อกันในแนวนอนแบบความสัมพันธ์ one-to-many

  • ความสัมพันธ์ one-to-many ที่ไม่ตั้งใจ
  • ความสัมพันธ์ many-to-many ที่ไม่ตั้งใจ

ปัญหาที่อาจเกิดจากการ concatenate:

DataFrame สองตัวที่รวมกันในแนวตั้งโดยมีแถวซ้ำใน DataFrame ที่สอง

  • อาจเกิดข้อมูลซ้ำโดยไม่ตั้งใจ
การรวมข้อมูลด้วย pandas

การตรวจสอบความถูกต้องของการ merge

.merge(validate=None):

  • ตรวจสอบว่า merge เป็นประเภทที่กำหนดหรือไม่
  • 'one_to_one'
  • 'one_to_many'
  • 'many_to_one'
  • 'many_to_many'
การรวมข้อมูลด้วย pandas

ชุดข้อมูลสำหรับตัวอย่างการ merge

ชื่อตาราง: tracks

  tid  name             aid  mtid  gid  u_price
0 2    Balls to the...  2    2     1    0.99   
1 3    Fast As a Shark  3    2     1    0.99   
2 4    Restless and...  3    2     1    0.99   

ชื่อตาราง: specs

  tid  milliseconds  bytes  
0 2    342562        5510424
1 3    230619        3990994
2 2    252051        4331779
การรวมข้อมูลด้วย pandas

Merge validate: one_to_one

tracks.merge(specs, on='tid', 
             validate='one_to_one')
Traceback (most recent call last):
MergeError: Merge keys are not unique in right dataset; not a one-to-one merge
การรวมข้อมูลด้วย pandas

Merge validate: one_to_many

albums.merge(tracks, on='aid', 
             validate='one_to_many')
  aid  title            artid  tid  name             mtid  gid  u_price
0 2    Balls to the...  2      2    Balls to the...  2     1    0.99   
1 3    Restless and...  2      3    Fast As a Shark  2     1    0.99   
2 3    Restless and...  2      4    Restless and...  2     1    0.99   
การรวมข้อมูลด้วย pandas

การตรวจสอบความถูกต้องของการ concatenate

.concat(verify_integrity=False):

  • ตรวจสอบว่า index ที่ได้จากการ concatenate มีค่าซ้ำหรือไม่
  • ค่าเริ่มต้นคือ False
การรวมข้อมูลด้วย pandas

ชุดข้อมูลสำหรับตัวอย่าง .concat()

ชื่อตาราง: inv_feb

     cid  invoice_date  total
iid 
7    38   2009-02-01    1.98 
8    40   2009-02-01    1.98 
9    42   2009-02-02    3.96 

ชื่อตาราง: inv_mar

     cid  invoice_date  total
iid 
9    17   2009-03-04    1.98 
15   19   2009-03-04    1.98 
16   21   2009-03-05    3.96 
การรวมข้อมูลด้วย pandas

ตัวอย่างการตรวจสอบความถูกต้องของการ concatenate

pd.concat([inv_feb, inv_mar], 
          verify_integrity=True)
Traceback (most recent call last):
ValueError: Indexes have overlapping 
values: Int64Index([9], dtype='int64', 
name='iid')
pd.concat([inv_feb, inv_mar], 
          verify_integrity=False)
     cid  invoice_date  total
iid 
7    38   2009-02-01    1.98 
8    40   2009-02-01    1.98 
9    42   2009-02-02    3.96 
9    17   2009-03-04    1.98 
15   19   2009-03-04    1.98 
16   21   2009-03-05    3.96
การรวมข้อมูลด้วย pandas

ทำไมต้องตรวจสอบความถูกต้อง และควรทำอย่างไร

เหตุผล:

  • ข้อมูลในโลกจริงมักจะ ไม่ สะอาด

สิ่งที่ควรทำ:

  • แก้ไขข้อมูลที่ผิดพลาด
  • ลบแถวที่ซ้ำกัน
การรวมข้อมูลด้วย pandas

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

การรวมข้อมูลด้วย pandas

Preparing Video For Download...