การใช้ merge_ordered()

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

Aaren Stubberfield

Instructor

merge_ordered()

ตัวอย่างตารางของ merge_ordered()

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

เปรียบเทียบเมธอด

เมธอด .merge():

  • คอลัมน์ที่ใช้ join
    • on, left_on, และ right_on
  • ประเภทของ join
    • how (left, right, inner, outer) {{@}}
    • ค่าเริ่มต้น inner
  • ชื่อคอลัมน์ที่ซ้ำกัน
    • suffixes
  • การเรียกใช้เมธอด
    • df1.merge(df2)

เมธอด merge_ordered():

  • คอลัมน์ที่ใช้ join
    • on, left_on, และ right_on
  • ประเภทของ join
    • how (left, right, inner, outer)
    • ค่าเริ่มต้น outer
  • ชื่อคอลัมน์ที่ซ้ำกัน
    • suffixes
  • การเรียกใช้ฟังก์ชัน
    • pd.merge_ordered(df1, df2)
การรวมข้อมูลด้วย pandas

ชุดข้อมูลทางการเงิน

ภาพหนังสือพิมพ์พร้อมกราฟ Dow Jones

1 Photo by Markus Spiske on Unsplash
การรวมข้อมูลด้วย pandas

ข้อมูลหุ้น

ชื่อตาราง: aapl

  date        close    
0 2007-02-01  12.087143
1 2007-03-01  13.272857
2 2007-04-01  14.257143
3 2007-05-01  17.312857
4 2007-06-01  17.434286

ชื่อตาราง: mcd

  date        close    
0 2007-01-01  44.349998
1 2007-02-01  43.689999
2 2007-03-01  45.049999
3 2007-04-01  48.279999
4 2007-05-01  50.549999
การรวมข้อมูลด้วย pandas

การ merge ข้อมูลหุ้น

import pandas as pd
pd.merge_ordered(aapl, mcd, on='date', suffixes=('_aapl','_mcd'))
  date        close_aapl  close_mcd
0 2007-01-01  NaN         44.349998
1 2007-02-01  12.087143   43.689999
2 2007-03-01  13.272857   45.049999
3 2007-04-01  14.257143   48.279999
4 2007-05-01  17.312857   50.549999
5 2007-06-01  17.434286   NaN
การรวมข้อมูลด้วย pandas

Forward fill

ภาพก่อนและหลังการใช้ forward fill ค่าที่หายไปในคอลัมน์จะถูกเติมด้วยค่าก่อนหน้าในคอลัมน์เดียวกัน

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

ตัวอย่าง forward fill

pd.merge_ordered(aapl, mcd, on='date', 
                 suffixes=('_aapl','_mcd'), 
                 fill_method='ffill')
  date        close_aapl  close_mcd
0 2007-01-01  NaN         44.349998
1 2007-02-01  12.087143   43.689999
2 2007-03-01  13.272857   45.049999
3 2007-04-01  14.257143   48.279999
4 2007-05-01  17.312857   50.549999
5 2007-06-01  17.434286   50.549999
pd.merge_ordered(aapl, mcd, on='date', 
                 suffixes=('_aapl','_mcd'))
  date        close_aapl  close_mcd
0 2007-01-01  NaN         44.349998
1 2007-02-01  12.087143   43.689999
2 2007-03-01  13.272857   45.049999
3 2007-04-01  14.257143   48.279999
4 2007-05-01  17.312857   50.549999
5 2007-06-01  17.434286   NaN
การรวมข้อมูลด้วย pandas

ควรใช้ merge_ordered() เมื่อใด?

  • ข้อมูลที่เรียงตามลำดับ / time series
  • การเติมค่าที่หายไป
การรวมข้อมูลด้วย pandas

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

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

Preparing Video For Download...