티스토리 뷰
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)(한글)
In [1]:
using DataFrames, Random # load package
Random.seed!(1); #srand(1);
데이터프레임의 행 다루기 (Manipulating rows of DataFrame)¶
행 재배치하기 (Reordering rows)¶
In [2]:
x = DataFrame(id=1:10, x = rand(10), y = [zeros(5); ones(5)]) # x[:x]가 크기 순이 아니길 바란다. ^^;
In [3]:
issorted(x), issorted(x, :x) # 데이터프레임이나 데이터프레임의 한 행이 크기순으로 정렬돼있는지를 확인한다.
Out[3]:
In [4]:
sort!(x, :x) # in place로 x를 :x를 기준으로 정렬한다.
Out[4]:
In [5]:
y = sort(x, :id) # :id 기준으로 정렬한 후 새로운 데이터프레임을 만든다.
Out[5]:
In [6]:
sort(x, (:y, :x), rev=(true, false)) # 행 2개를 기준으로 정렬하는데, 첫번째는 내림차순, 두 번째는 오름차순 이다.
Out[6]:
In [7]:
sort(x, (order(:y, rev=true), :x)) # 위와 같은 작업을 하는 코드이다.
Out[7]:
In [8]:
sort(x, (order(:y, rev=true), order(:x, by=v->-v))) # 더 있어보이는 정렬방법이다.
Out[8]:
In [9]:
x[shuffle(1:10), :] # index를 이용해서 정렬하는 방법이다. (여기서는 무작위로 섞었다.)
Out[9]:
In [10]:
sort!(x, :id)
x[[1,10],:] = x[[10,1],:] # 행을 바꿨다.
x
Out[10]:
In [11]:
x[1,:], x[10,:] = x[10,:], x[1,:] # 행을 또 다시 바꿨다.
x
Out[11]:
행 합치기 또는 추가하기 (Merging/adding rows)¶
In [12]:
x = DataFrame(rand(3, 5))
Out[12]:
In [13]:
[x; x] # 행 합치기다. 반드시 열의 수가 같아야한다. (vcat과 같다.)
Out[13]:
In [14]:
y = x[reverse(names(x))] # 행의 순서를 바꿔서 y로 저장했다.
Out[14]:
In [15]:
vcat(x, y) # vcat을 이용하면 이름을 매칭시켜서 합친다.
Out[15]:
In [16]:
vcat(x, y[1:3]) # 그러나 열 이름이 여전히 매치되어야한다.
In [17]:
append!(x, x) # 깉은 결과를 내지만 x 자체를 수정한다.
Out[17]:
In [18]:
append!(x, y) # 여기서는 열 이름이 반드시 정확하게 같이야한다.
In [19]:
push!(x, 1:5) # 행 하나를 추가하는 방법이다. 반드시 열의 숫자와 같아야하고 type도 맞아야한다.
x
Out[19]:
In [20]:
push!(x, Dict(:x1=> 11, :x2=> 12, :x3=> 13, :x4=> 14, :x5=> 15)) # 딕셔너리(dictionary)를 이용해서도 할 수 있다.
x
Out[20]:
일부 행 가져오기 또는 행 지우기 (Subsetting/removing rows)¶
In [21]:
x = DataFrame(id=1:10, val='a':'j')
Out[21]:
In [22]:
x[1:2, :] # 인덱스(index)로 가져오기.
Out[22]:
In [23]:
view(x, 1:2) # 같은 방법이지만 view를 이용했다.
Out[23]:
In [24]:
x[repeat([true, false], 5), :] # 논리값(Bool)을 이용하기. 길이가 정확하게 일치해야 한다.
#x[repmat([true, false], 5), :]
Out[24]:
In [25]:
view(x, repeat([true, false], 5), :) # 다시 view를 이용해서 보기.
#view(x, repmat([true, false], 5), :)
Out[25]:
In [26]:
deleterows!(x, 7) # 행 하나 지우기
Out[26]:
In [27]:
deleterows!(x, 6:7) # 여러 행 지우기
Out[27]:
In [28]:
x = DataFrame([1:4, 2:5, 3:6])
Out[28]:
In [29]:
filter(r -> r[:x1] > 2.5, x) # filter를 이용해서 조건에 맞는 행 골라오기
Out[29]:
In [30]:
# do-block 구문을 이용해서 in place로 x 수정하는 예시.
filter!(x) do r
if r[:x1] > 2.5
return r[:x2] < 4.5
end
r[:x3] < 3.5
end
Out[30]:
중복삭제 (Deduplicating)¶
In [31]:
x = DataFrame(A=[1,2], B=["x","y"])
append!(x, x)
x[:C] = 1:4
x
Out[31]:
In [32]:
unique(x, [1,2]) # 주어진 index에서 유일한 첫번째 열 가져오기
Out[32]:
In [33]:
unique(x) # 지금은 모든 열을 가져온다.
Out[33]:
In [34]:
nonunique(x, :A) # 유일하지 않은 열의 지시자(indeicator)를 알 수 있다.
Out[34]:
In [35]:
unique!(x, :B) # in place로 x를 수정한다.
Out[35]:
데이터프레임
의 한 열을 벡터로 불러오기 (Extracting one row from DataFrame
into a vector)¶
In [36]:
x = DataFrame(x=[1,missing,2], y=["a", "b", missing], z=[true,false,true])
Out[36]:
In [37]:
cols = [:x, :y]
[x[1, col] for col in cols] # 행의 일부
Out[37]:
In [38]:
[[x[i, col] for col in names(x)] for i in 1:nrow(x)] # 벡터의 벡터. 각 원소는 x의 한 행 전체이다.
Out[38]:
In [39]:
Tuple(x[1, col] for col in cols) # 비슷하게 튜플(Tuple)로 가져올 수도 있다.
Out[39]:
'Flux in Julia > Learning Julia (Intro_to_Julia_DFs)' 카테고리의 다른 글
07. factors (한글) (0) | 2018.10.13 |
---|---|
07. factors (0) | 2018.10.13 |
06. rows (0) | 2018.10.12 |
05. columns (한글) (0) | 2018.10.11 |
05. columns (0) | 2018.10.11 |