Flux in Julia/Learning Julia (Intro_to_Julia_DFs)
08. joins (한글)
딥스탯
2018. 10. 14. 14:42
Introduction to DataFrames¶
Bogumił Kamiński, Apr 21, 2017
출처¶
함께보기¶
- https://deepstat.tistory.com/69 (01. constructors)(in English)
- https://deepstat.tistory.com/70 (01. constructors)(한글)
- https://deepstat.tistory.com/71 (02. basicinfo)(in English)
- https://deepstat.tistory.com/72 (02. basicinfo)(한글)
- https://deepstat.tistory.com/73 (03. missingvalues)(in English)
- https://deepstat.tistory.com/74 (03. missingvalues)(한글)
- https://deepstat.tistory.com/75 (04. loadsave)(in English)
- https://deepstat.tistory.com/76 (04. loadsave)(한글)
- https://deepstat.tistory.com/77 (05. columns)(in English)
- https://deepstat.tistory.com/78 (05. columns)(한글)
- https://deepstat.tistory.com/79 (06. rows)(in English)
- https://deepstat.tistory.com/80 (06. rows)(한글)
- https://deepstat.tistory.com/81 (07. factors)(in English)
- https://deepstat.tistory.com/82 (07. factors)(한글)
- https://deepstat.tistory.com/83 (08. joins)(in English)
- https://deepstat.tistory.com/84 (08. joins)(한글)
In [1]:
using DataFrames # load package
데이터프레임 조인하기 (Joining DataFrames)¶
조인할 데이터프레임 준비하기 (Preparing DataFrames for a join)¶
In [2]:
x = DataFrame(ID=[1,2,3,4,missing], name = ["Alice", "Bob", "Conor", "Dave","Zed"])
y = DataFrame(id=[1,2,5,6,missing], age = [21,22,23,24,99])
println(x)
println(y)
In [3]:
rename!(x, :ID=>:id) # 조인(joini할 기준이 되는 행 이름은 같아야만 한다.
Out[3]:
기본 조인 (Standard joins: inner, left, right, outer, semi, anti)¶
In [4]:
join(x, y, on=:id) # 기본적으로 이너조인(inner join)을 수행한다. 결측(missing)도 조인된다.
Out[4]:
In [5]:
join(x, y, on=:id, kind=:left) # 레프트조인(left join)
Out[5]:
In [6]:
join(x, y, on=:id, kind=:right) # 라이트조인(right join)
Out[6]:
In [7]:
join(x, y, on=:id, kind=:outer) #아우터조인(outer join)
Out[7]:
In [8]:
join(x, y, on=:id, kind=:semi) #세미조인(semi join)
Out[8]:
In [9]:
join(x, y, on=:id, kind=:anti) #안티조인(anti join)
Out[9]:
크로스조인 (Cross join)¶
In [10]:
# 크로스조인(cross-join)은 "on" 인자(argument)를 필요로 하지 않는다.
# 크로스조인(cross-join)은 카테이션 곱(Cartesian product) 혹은 인자(argument)를 만든다.
function expand_grid(;xs...) # R 언어에서 쓰이는 expand.grid의 간단한 형태의 함수
reduce((x,y) -> join(x, DataFrame(Pair(y...)), kind=:cross),
DataFrame(Pair(xs[1]...)), xs[2:end])
end
expand_grid(a=[1,2], b=["a","b","c"], c=[true,false])
In [11]:
?reduce
Out[11]:
복잡한 형태의 조인 (Complex cases of joins)¶
In [12]:
x = DataFrame(id1=[1,1,2,2,missing,missing],
id2=[1,11,2,21,missing,99],
name = ["Alice", "Bob", "Conor", "Dave","Zed", "Zoe"])
y = DataFrame(id1=[1,1,3,3,missing,missing],
id2=[11,1,31,3,missing,999],
age = [21,22,23,24,99, 100])
println(x)
println(y)
In [13]:
join(x, y, on=[:id1, :id2]) # 2개 행을 기준으로 조인
Out[13]:
In [14]:
join(x, y, on=[:id1], makeunique=true) # 중복되는 경우 모든 경우의 결합을 다 만들어준다. (이 예제는 이너조인(inner join))
Out[14]:
In [15]:
join(x, y, on=[:id1], kind=:semi) # 예외적으로 세미조인(semi join)인 경우는 모든 결합을 다 만들어주지 않는다.
Out[15]: