티스토리 뷰

12_pitfalls(한글)

Introduction to DataFrames

Bogumił Kamiński, Apr 21, 2018

Reference

Series

In [1]:
using DataFrames

가능한 함정들 (Possible pitfalls)

데이터프레임을 만들 때 무엇이 복사되는지 알아야 한다.

In [2]:
x = DataFrame(rand(3, 5))
Out[2]:
x1x2x3x4x5
Float64Float64Float64Float64Float64
10.6744340.3832040.7420570.6070630.312016
20.2660330.9231040.4044890.4209660.0165625
30.1480820.9387920.8198370.8691530.417423
In [3]:
y = DataFrame(x)
x === y # 복사된 게 아니다. (같은 객체(object)다.)
┌ Warning: In the future DataFrame constructor called with a `DataFrame` argument will return a copy. Use `convert(DataFrame, df)` to avoid copying if `df` is a `DataFrame`.
│   caller = top-level scope at In[3]:1
└ @ Core In[3]:1
Out[3]:
true
In [4]:
y = copy(x)
x === y # 같은 객체(object)가 아니다.
Out[4]:
false
In [5]:
all(x[i] === y[i] for i in ncol(x)) # 그러나 열들은 같다.
Out[5]:
true
In [6]:
x = 1:3; y = [1, 2, 3]; df = DataFrame(x=x,y=y) # 배열(array)를 만들거나 행을 넣을 때도 마찬가지다. (범위(range)를 제외하고)
Out[6]:
xy
Int64Int64
111
222
333
In [7]:
y === df[:y] # 같은 객체다.
Out[7]:
true
In [8]:
typeof(x), typeof(df[:x]) # 범위(range) 는 벡터(vector)로 바뀐다.
Out[8]:
(UnitRange{Int64}, Array{Int64,1})

그룹화된 데이터프레임의 부모객체(parent)를 수정하지 마라.

In [9]:
x = DataFrame(id=repeat([1,2], outer=3), x=1:6)
g = groupby(x, :id)
Out[9]:
GroupedDataFrame with 2 groups based on key: :id
First Group: 3 rows
│ Row │ id    │ x     │
│     │ Int64Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 1     │
│ 2   │ 1     │ 3     │
│ 3   │ 1     │ 5     │
⋮
Last Group: 3 rows
│ Row │ id    │ x     │
│     │ Int64Int64 │
├─────┼───────┼───────┤
│ 1   │ 2     │ 2     │
│ 2   │ 2     │ 4     │
│ 3   │ 2     │ 6     │
In [10]:
x[1:3, 1]=[2,2,2]
g # 이제 결과가 잘못됐다. g는 단지 뷰(view)일 뿐이다.
Out[10]:
GroupedDataFrame with 2 groups based on key: :id
First Group: 3 rows
│ Row │ id    │ x     │
│     │ Int64Int64 │
├─────┼───────┼───────┤
│ 1   │ 2     │ 1     │
│ 2   │ 2     │ 3     │
│ 3   │ 1     │ 5     │
⋮
Last Group: 3 rows
│ Row │ id    │ x     │
│     │ Int64Int64 │
├─────┼───────┼───────┤
│ 1   │ 2     │ 2     │
│ 2   │ 2     │ 4     │
│ 3   │ 2     │ 6     │

데이터프레임의 열을 선택할 때 논리값(boolean)을 이용할 수도 있음을 기억해라.

In [11]:
using Random
Random.seed!(1)
x = DataFrame(rand(5, 5))
Out[11]:
x1x2x3x4x5
Float64Float64Float64Float64Float64
10.2360330.2109680.5557510.2094720.0769509
20.3465170.9519160.4371080.2513790.640396
30.3127070.9999050.4247180.02037490.873544
40.007909280.2516620.7732230.2877020.278582
50.4886130.9866660.281190.8595120.751313
In [12]:
x[x[:x1] .< 0.25] # 행별로가 아닌 열별로 선택했다. (열 수와 행 수가 같아서 우연히 작동할 수 있었다.)
Out[12]:
x1x4
Float64Float64
10.2360330.209472
20.3465170.251379
30.3127070.0203749
40.007909280.287702
50.4886130.859512
In [13]:
x[x[:x1] .< 0.25, :] # 아마 이게 우리가 원한 것일 거다.
Out[13]:
x1x2x3x4x5
Float64Float64Float64Float64Float64
10.2360330.2109680.5557510.2094720.0769509
20.007909280.2516620.7732230.2877020.278582

데이터프레임의 열 선택은 명시적으로 복사(explicit copy)하지 않으면 별칭(alias)을 만든다.

In [14]:
x = DataFrame(a=1:3)
x[:b] = x[1] # 별칭(alias)
x[:c] = x[:, 1] # 이 또한 별칭
x[:d] = x[1][:] # 복사
x[:e] = copy(x[1]) # 명시적 복사(explicit copy)
display(x)
x[1,1] = 100
display(x)
abcde
Int64Int64Int64Int64Int64
111111
222222
333333
abcde
Int64Int64Int64Int64Int64
110010010011
222222
333333
┌ Warning: indexing with colon as row will create a copy in the future use df[col_inds] to get the columns without copying
│   caller = top-level scope at In[14]:3
└ @ Core In[14]:3

'Flux in Julia > Learning Julia (Intro_to_Julia_DFs)' 카테고리의 다른 글

13. extras (한글)  (0) 2018.10.20
13. extras  (0) 2018.10.20
12. pitfalls  (0) 2018.10.19
11. performance (한글)  (0) 2018.10.18
11. performance  (0) 2018.10.18
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함