티스토리 뷰
Introduction to DataFrames¶
Bogumił Kamiński, Apr 21, 2018
출처¶
함께보기¶
- 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)(한글)
- https://deepstat.tistory.com/85 (09. reshaping)(in English)
- https://deepstat.tistory.com/86 (09. reshaping)(한글)
- https://deepstat.tistory.com/87 (10. transforms)(in English)
- https://deepstat.tistory.com/88 (10. transforms)(한글)
In [1]:
using DataFrames # load package
분할-적용-결합 (Split-apply-combine)¶
In [2]:
x = DataFrame(id=[1,2,3,4,1,2,3,4], id2=[1,2,1,2,1,2,1,2], v=rand(8))
Out[2]:
In [3]:
gx1 = groupby(x, :id)
Out[3]:
In [4]:
gx2 = groupby(x, [:id, :id2])
Out[4]:
In [5]:
vcat(gx2...) # 원래의 데이터프레임으로.
Out[5]:
In [6]:
x = DataFrame(id = [missing, 5, 1, 3, missing], x = 1:5)
Out[6]:
In [7]:
show(groupby(x, :id), allgroups=true) # 기본적으로 그룹은 결측(missing)도 포함하고, 정렬되지 않는다.
In [8]:
show(groupby(x, :id, sort=true, skipmissing=true), allgroups=true) # 하지만 바꿀 수 있다.
In [9]:
x = DataFrame(id=rand('a':'d', 100), v=rand(100));
using Statistics
by(x, :id, y->mean(y[:v])) # 각각의 그룹에 대해서 함수를 적용할 수 있다.
Out[9]:
In [10]:
by(x, :id, y->mean(y[:v]), sort=true) # 결과를 정렬할 수 있다.
Out[10]:
In [11]:
by(x, :id, y->DataFrame(res=mean(y[:v]))) # 이 방법으로 열 이름을 바꿀 수 있다.
Out[11]:
In [12]:
x = DataFrame(id=rand('a':'d', 100), x1=rand(100), x2=rand(100))
aggregate(x, :id, sum) # 모든 열에 대해서 :id별로 함수를 적용한다.
Out[12]:
In [13]:
aggregate(x, :id, sum, sort=true) # 이 또한 정렬할 수 있다.
Out[13]:
원문의 저자가 map과 combine은 유용한지 잘 모르겠어서 크게 언급하지 않기로 했다고 한다. (by가 더 낫다고 한다.)
In [14]:
x = DataFrame(rand(3, 5))
Out[14]:
In [15]:
map(mean, eachcol(x)) # 각 열에 대해서 함수를 매핑하고 결과를 데이터프레임으로 받는다.
Out[15]:
In [16]:
foreach(c -> println(c[1], ": ", mean(c[2])), eachcol(x)) # 반복을 이용해서 열 이름과 계산 값을 튜플로 받는다.
In [17]:
colwise(mean, x) # 열별로 하는 것은 비슷하나, 결과를 벡터로 받는다.
Out[17]:
In [18]:
x[:id] = [1,1,2]
colwise(mean,groupby(x, :id)) # 그룹화된 데이터프레임 (GroupedDataFrame)으로 작업한다.
Out[18]:
In [19]:
map(r -> r[:x1]/r[:x2], eachrow(x)) # 이번에는 데이터프레임의 행별로 함수를 적용한다.
Out[19]:
'Flux in Julia > Learning Julia (Intro_to_Julia_DFs)' 카테고리의 다른 글
11. performance (한글) (0) | 2018.10.18 |
---|---|
11. performance (0) | 2018.10.18 |
10. transforms (0) | 2018.10.16 |
09. reshaping(한글) (0) | 2018.10.15 |
09. reshaping (0) | 2018.10.15 |