合併多個資料集

使用 pandas 的精實資料導入

Amany Mahfouz

Instructor

串接(Concatenating)

  • 使用情境:將一個 dataframe 的列加到另一個
  • concat()
    • pandas 函式
    • 語法:pd.concat([df1,df2])
    • ignore_index 設為 True 可重新編號列
使用 pandas 的精實資料導入

串接(Concatenating)

# Get first 20 bookstore results
params = {"term": "bookstore", 
          "location": "San Francisco"}  
first_results = requests.get(api_url,
                             headers=headers,
                             params=params).json()

first_20_bookstores = json_normalize(first_results["businesses"],
                                     sep="_")

print(first_20_bookstores.shape)
(20, 24)
使用 pandas 的精實資料導入
# Get the next 20 bookstores
params["offset"] = 20
next_results = requests.get(api_url,
                            headers=headers,
                            params=params).json()

next_20_bookstores = json_normalize(next_results["businesses"],
                                    sep="_")

print(next_20_bookstores.shape)
(20, 24)
使用 pandas 的精實資料導入
# Put bookstore datasets together, renumber rows
bookstores = pd.concat([first_20_bookstores, next_20_bookstores],
                        ignore_index=True)

print(bookstores.name)
0                             City Lights Bookstore
1                            Alexander Book Company
2                                 Borderlands Books
3                                   Alley Cat Books
4                                   Dog Eared Books
...                                             ...
35                                     Forest Books
36                San Francisco Center For The Book
37                           KingSpoke - Book Store
38                            Eastwind Books & Arts
39                                      My Favorite
Name: name, dtype: object
使用 pandas 的精實資料導入

合併(Merging)

  • 使用情境:合併資料集以新增相關欄位
  • 資料集具有共同值的鍵欄位
  • merge()pandas 中的 SQL join 版本
使用 pandas 的精實資料導入

合併(Merging)

  • merge()
    • 既是 pandas 函式也是 dataframe 方法
  • df.merge() 參數
    • 要合併的第二個 dataframe
    • 要用來合併的欄位
      • 兩邊欄名相同用 on
      • 欄名不同用 left_onright_on
      • 鍵欄位的資料型別需一致
使用 pandas 的精實資料導入
call_counts.head()
  created_date  call_counts
0   01/01/2018         4597
1   01/02/2018         4362
2   01/03/2018         3045
3   01/04/2018         3374
4   01/05/2018         4333
weather.head()
         date    tmax  tmin  
0  12/01/2017      52    42  
1  12/02/2017      48    39  
2  12/03/2017      48    42  
3  12/04/2017      51    40  
4  12/05/2017      61    50
使用 pandas 的精實資料導入

合併(Merging)

# Merge weather into call counts on date columns
merged = call_counts.merge(weather, 
                           left_on="created_date", 
                           right_on="date")

print(merged.head())
  created_date  call_counts        date  tmax  tmin
0   01/01/2018         4597  01/01/2018    19     7
1   01/02/2018         4362  01/02/2018    26    13
2   01/03/2018         3045  01/03/2018    30    16
3   01/04/2018         3374  01/04/2018    29    19
4   01/05/2018         4333  01/05/2018    19     9
使用 pandas 的精實資料導入

合併(Merging)

   created_date  call_counts        date  tmax  tmin
0    01/01/2018         4597  01/01/2018    19     7
1    01/02/2018         4362  01/02/2018    26    13
2    01/03/2018         3045  01/03/2018    30    16
3    01/04/2018         3374  01/04/2018    29    19
4    01/05/2018         4333  01/05/2018    19     9
  • merge() 預設:只回傳兩個資料集中皆存在的值
  • 每個 dataframe 的值配對會得到一筆紀錄
    • 多重配對=多筆紀錄
使用 pandas 的精實資料導入

一起來練習吧!

使用 pandas 的精實資料導入

Preparing Video For Download...