p175

choco·2023년 11월 6일

pandas

목록 보기
1/3

reference
https://www.snugarchive.com/blog/python-pandas-guide-3/
https://programmerpsy.tistory.com/17

Input / Output

# 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

# inner join, 교집합만 추출
df = pd.merge(person, address) 

outer join

df = pd.merge(person, address, how='outer') 

left join

# left join
df = pd.merge(person, address, how = 'left', on = 'personId')

code

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
profile
choco_lee

0개의 댓글