reference
https://www.snugarchive.com/blog/python-pandas-guide-3/
https://programmerpsy.tistory.com/17
# Person table:
# +----------+----------+-----------+
# | personId | lastName | firstName |
# +----------+----------+-----------+
# | 1 | Wang | Allen |
# | 2 | Alice | Bob |
#+----------+----------+-----------+
# Address table:
# +-----------+----------+---------------+------------+
# | addressId | personId | city | state |
# +-----------+----------+---------------+------------+
# | 1 | 2 | New York City | New York |
# | 2 | 3 | Leetcode | California |
# +-----------+----------+---------------+------------+
# Output:
# +-----------+----------+---------------+----------+
# | firstName | lastName | city | state |
# +-----------+----------+---------------+----------+
# | Allen | Wang | Null | Null |
# | Bob | Alice | New York City | New York |
# +-----------+----------+---------------+----------+
#
import pandas as pd
person = [[1, "Wang", "Allen"], [2, "Alice", "Bob"]]
address = [[1, 2, "New York", "New York"], [2, 3, "Leetcode", "California"]]
person = pd.DataFrame(person, columns = ["personId", "lastName" , "firstName"])
address = pd.DataFrame(address, columns = ["addressId", "personId", "city", "state"])
# inner join, 교집합만 추출
df = pd.merge(person, address)
df = pd.merge(person, address, how='outer')
# left join
df = pd.merge(person, address, how = 'left', on = 'personId')
def combine_two_tables(person: pd.DataFrame, address: pd.DataFrame) -> pd.DataFrame:
# left join
df = pd.merge(left=person, right = address, how = 'left', on = 'personId')
df = df[['lastName', 'firstName', 'city', 'state']]
return df