複数のデータセットを結合する

pandasで効率よくデータを取り込む

Amany Mahfouz

Instructor

連結(Concatenate)

  • 用途: 片方のDataFrameの行をもう一方に追加
  • concat()
    • pandasの関数
    • 構文: pd.concat([df1,df2])
    • 行番号を振り直すには ignore_indexTrue に設定
pandasで効率よくデータを取り込む

連結(Concatenate)

# 本屋の最初の20件を取得
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で効率よくデータを取り込む
# 次の20件を取得
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で効率よくデータを取り込む
# 本屋のデータセットを結合し、行番号を振り直す
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で効率よくデータを取り込む

結合(Merge)

  • 用途: 関連列を追加するために結合
  • 共通の値を持つキー列がある
  • merge(): SQLのJOINに相当するpandasの機能
pandasで効率よくデータを取り込む

結合(Merge)

  • 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で効率よくデータを取り込む

結合(Merge)

# 日付列で weather を call_counts に結合
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で効率よくデータを取り込む

結合(Merge)

   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間で値が一致するたびに1行
    • 複数一致なら複数行
pandasで効率よくデータを取り込む

¡Vamos a practicar!

pandasで効率よくデータを取り込む

Preparing Video For Download...