여러 데이터셋 결합

pandas로 빠르고 간편한 데이터 적재

Amany Mahfouz

Instructor

연결(Concatenating)

  • 사용 사례: 한 데이터프레임에 다른 데이터프레임의 행 추가
  • concat()
    • pandas 함수
    • 구문: pd.concat([df1,df2])
    • 행 번호 재지정은 ignore_index=True
pandas로 빠르고 간편한 데이터 적재

연결(Concatenating)

# 첫 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로 빠르고 간편한 데이터 적재

병합(Merging)

  • 사용 사례: 관련 열을 추가하려고 데이터셋 결합
  • 공통 값을 가진 키 열이 있음
  • merge(): SQL 조인의 pandas 버전
pandas로 빠르고 간편한 데이터 적재

병합(Merging)

  • merge()
    • pandas 함수이자 데이터프레임 메서드
  • df.merge() 인수
    • 병합할 두 번째 데이터프레임
    • 병합 기준 열
      • 두 DF에서 이름이 같으면 on
      • 키 이름이 다르면 left_on, right_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)

# 날짜 열로 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로 빠르고 간편한 데이터 적재

병합(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() 동작: 두 데이터셋 모두에 있는 값만 반환
  • 데이터프레임 간 값이 일치할 때마다 한 레코드
    • 다중 일치 = 여러 레코드
pandas로 빠르고 간편한 데이터 적재

연습해 봅시다!

pandas로 빠르고 간편한 데이터 적재

Preparing Video For Download...