Transformation to DataFrames#
Split-apply-combine
using DataFrames
Grouping a data frame#
groupby
x = DataFrame(id=[1, 2, 3, 4, 1, 2, 3, 4], id2=[1, 2, 1, 2, 1, 2, 1, 2], v=rand(8))
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 2 | 2 | 0.369038 |
3 | 3 | 1 | 0.814633 |
4 | 4 | 2 | 0.377291 |
5 | 1 | 1 | 0.0800426 |
6 | 2 | 2 | 0.796786 |
7 | 3 | 1 | 0.21076 |
8 | 4 | 2 | 0.75457 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 1 | 1 | 0.0800426 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.377291 |
2 | 4 | 2 | 0.75457 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 2 | 2 | 0.369038 |
3 | 3 | 1 | 0.814633 |
4 | 4 | 2 | 0.377291 |
5 | 1 | 1 | 0.0800426 |
6 | 2 | 2 | 0.796786 |
7 | 3 | 1 | 0.21076 |
8 | 4 | 2 | 0.75457 |
gx2 = groupby(x, [:id, :id2])
GroupedDataFrame with 4 groups based on keys: id, id2
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 1 | 1 | 0.0800426 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.377291 |
2 | 4 | 2 | 0.75457 |
get the parent DataFrame
parent(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 2 | 2 | 0.369038 |
3 | 3 | 1 | 0.814633 |
4 | 4 | 2 | 0.377291 |
5 | 1 | 1 | 0.0800426 |
6 | 2 | 2 | 0.796786 |
7 | 3 | 1 | 0.21076 |
8 | 4 | 2 | 0.75457 |
back to the DataFrame, but in a different order of rows than the original
vcat(gx2...)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 1 | 1 | 0.0800426 |
3 | 2 | 2 | 0.369038 |
4 | 2 | 2 | 0.796786 |
5 | 3 | 1 | 0.814633 |
6 | 3 | 1 | 0.21076 |
7 | 4 | 2 | 0.377291 |
8 | 4 | 2 | 0.75457 |
the same as above
DataFrame(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 1 | 1 | 0.0800426 |
3 | 2 | 2 | 0.369038 |
4 | 2 | 2 | 0.796786 |
5 | 3 | 1 | 0.814633 |
6 | 3 | 1 | 0.21076 |
7 | 4 | 2 | 0.377291 |
8 | 4 | 2 | 0.75457 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
Row | v |
---|---|
Float64 | |
1 | 0.325215 |
2 | 0.0800426 |
3 | 0.369038 |
4 | 0.796786 |
5 | 0.814633 |
6 | 0.21076 |
7 | 0.377291 |
8 | 0.75457 |
vector of names of grouping variables
groupcols(gx2)
2-element Vector{Symbol}:
:id
:id2
and non-grouping variables
valuecols(gx2)
1-element Vector{Symbol}:
:v
group indices in parent(gx2)
groupindices(gx2)
8-element Vector{Union{Missing, Int64}}:
1
2
3
4
1
2
3
4
kgx2 = keys(gx2)
4-element DataFrames.GroupKeys{DataFrames.GroupedDataFrame{DataFrames.DataFrame}}:
GroupKey: (id = 1, id2 = 1)
GroupKey: (id = 2, id2 = 2)
GroupKey: (id = 3, id2 = 1)
GroupKey: (id = 4, id2 = 2)
You can index into a GroupedDataFrame
like to a vector or to a dictionary. The second form accepts GroupKey
, NamedTuple
or a Tuple
.
gx2
GroupedDataFrame with 4 groups based on keys: id, id2
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.325215 |
2 | 1 | 1 | 0.0800426 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.377291 |
2 | 4 | 2 | 0.75457 |
k = keys(gx2)[1]
GroupKey: (id = 1, id2 = 1)
ntk = NamedTuple(k)
(id = 1, id2 = 1)
tk = Tuple(k)
(1, 1)
the operations below produce the same result and are proformant
gx2[1], gx2[k], gx2[ntk], gx2[tk]
(2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.325215
2 │ 1 1 0.0800426, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.325215
2 │ 1 1 0.0800426, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.325215
2 │ 1 1 0.0800426, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.325215
2 │ 1 1 0.0800426)
handling missing values
x = DataFrame(id=[missing, 5, 1, 3, missing], x=1:5)
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | missing | 1 |
2 | 5 | 2 |
3 | 1 | 3 |
4 | 3 | 4 |
5 | missing | 5 |
by default groups include missing values and their order is not guaranteed
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | 1 | 3 |
⋮
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | missing | 1 |
2 | missing | 5 |
but we can change it; now they are sorted
groupby(x, :id, sort=true, skipmissing=true)
GroupedDataFrame with 3 groups based on key: id
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | 1 | 3 |
⋮
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | 5 | 2 |
and now they are in the order they appear in the source data frame
groupby(x, :id, sort=false)
GroupedDataFrame with 4 groups based on key: id
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | missing | 1 |
2 | missing | 5 |
⋮
Row | id | x |
---|---|---|
Int64? | Int64 | |
1 | 3 | 4 |
Performing transformations#
by group using combine, select, select!, transform, and transform!
using Statistics
using Chain
x = DataFrame(id=rand('a':'d', 100), v=rand(100))
Row | id | v |
---|---|---|
Char | Float64 | |
1 | d | 0.911682 |
2 | a | 0.336318 |
3 | c | 0.569249 |
4 | a | 0.860833 |
5 | d | 0.990385 |
6 | d | 0.671075 |
7 | b | 0.456261 |
8 | a | 0.663899 |
9 | a | 0.673223 |
10 | c | 0.822798 |
11 | b | 0.948415 |
12 | d | 0.462216 |
13 | b | 0.772046 |
⋮ | ⋮ | ⋮ |
89 | b | 0.736596 |
90 | c | 0.010389 |
91 | d | 0.958571 |
92 | b | 0.132037 |
93 | c | 0.132203 |
94 | d | 0.6734 |
95 | c | 0.581931 |
96 | d | 0.732172 |
97 | a | 0.647178 |
98 | a | 0.746204 |
99 | a | 0.0444478 |
100 | c | 0.206397 |
apply a function to each group of a data frame combine keeps as many rows as are returned from the function
@chain x begin
groupby(:id)
combine(:v => mean)
end
Row | id | v_mean |
---|---|---|
Char | Float64 | |
1 | d | 0.579567 |
2 | a | 0.50468 |
3 | c | 0.512914 |
4 | b | 0.584901 |
x.id2 = axes(x, 1)
Base.OneTo(100)
Select and transform keep as many rows as are in the source data frame and in correct order. Additionally, transform keeps all columns from the source.
@chain x begin
groupby(:id)
transform(:v => mean)
end
Row | id | v | id2 | v_mean |
---|---|---|---|---|
Char | Float64 | Int64 | Float64 | |
1 | d | 0.911682 | 1 | 0.579567 |
2 | a | 0.336318 | 2 | 0.50468 |
3 | c | 0.569249 | 3 | 0.512914 |
4 | a | 0.860833 | 4 | 0.50468 |
5 | d | 0.990385 | 5 | 0.579567 |
6 | d | 0.671075 | 6 | 0.579567 |
7 | b | 0.456261 | 7 | 0.584901 |
8 | a | 0.663899 | 8 | 0.50468 |
9 | a | 0.673223 | 9 | 0.50468 |
10 | c | 0.822798 | 10 | 0.512914 |
11 | b | 0.948415 | 11 | 0.584901 |
12 | d | 0.462216 | 12 | 0.579567 |
13 | b | 0.772046 | 13 | 0.584901 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
89 | b | 0.736596 | 89 | 0.584901 |
90 | c | 0.010389 | 90 | 0.512914 |
91 | d | 0.958571 | 91 | 0.579567 |
92 | b | 0.132037 | 92 | 0.584901 |
93 | c | 0.132203 | 93 | 0.512914 |
94 | d | 0.6734 | 94 | 0.579567 |
95 | c | 0.581931 | 95 | 0.512914 |
96 | d | 0.732172 | 96 | 0.579567 |
97 | a | 0.647178 | 97 | 0.50468 |
98 | a | 0.746204 | 98 | 0.50468 |
99 | a | 0.0444478 | 99 | 0.50468 |
100 | c | 0.206397 | 100 | 0.512914 |
note that combine reorders rows by group of GroupedDataFrame
@chain x begin
groupby(:id)
combine(:id2, :v => mean)
end
Row | id | id2 | v_mean |
---|---|---|---|
Char | Int64 | Float64 | |
1 | d | 1 | 0.579567 |
2 | d | 5 | 0.579567 |
3 | d | 6 | 0.579567 |
4 | d | 12 | 0.579567 |
5 | d | 16 | 0.579567 |
6 | d | 22 | 0.579567 |
7 | d | 28 | 0.579567 |
8 | d | 30 | 0.579567 |
9 | d | 35 | 0.579567 |
10 | d | 37 | 0.579567 |
11 | d | 38 | 0.579567 |
12 | d | 39 | 0.579567 |
13 | d | 40 | 0.579567 |
⋮ | ⋮ | ⋮ | ⋮ |
89 | b | 58 | 0.584901 |
90 | b | 59 | 0.584901 |
91 | b | 64 | 0.584901 |
92 | b | 65 | 0.584901 |
93 | b | 69 | 0.584901 |
94 | b | 71 | 0.584901 |
95 | b | 74 | 0.584901 |
96 | b | 77 | 0.584901 |
97 | b | 84 | 0.584901 |
98 | b | 88 | 0.584901 |
99 | b | 89 | 0.584901 |
100 | b | 92 | 0.584901 |
we give a custom name for the result column
@chain x begin
groupby(:id)
combine(:v => mean => :res)
end
Row | id | res |
---|---|---|
Char | Float64 | |
1 | d | 0.579567 |
2 | a | 0.50468 |
3 | c | 0.512914 |
4 | b | 0.584901 |
you can have multiple operations
@chain x begin
groupby(:id)
combine(:v => mean => :res1, :v => sum => :res2, nrow => :n)
end
Row | id | res1 | res2 | n |
---|---|---|---|---|
Char | Float64 | Float64 | Int64 | |
1 | d | 0.579567 | 15.6483 | 27 |
2 | a | 0.50468 | 9.08424 | 18 |
3 | c | 0.512914 | 13.8487 | 27 |
4 | b | 0.584901 | 16.3772 | 28 |
Additional notes:
select!
andtransform!
perform operations in-placeThe general syntax for transformation is
source_columns => function => target_column
if you pass multiple columns to a function they are treated as positional arguments
ByRow
andAsTable
work exactly like discussed for operations on data frames in 05_columns.ipynbyou can automatically groupby again the result of
combine
,select
etc. by passingungroup=false
keyword argument to themsimilarly
keepkeys
keyword argument allows you to drop grouping columns from the resulting data frame
It is also allowed to pass a function to all these functions (also - as a special case, as a first argument). In this case the return value can be a table. In particular it allows for an easy dropping of groups if you return an empty table from the function.
If you pass a function you can use a do
block syntax. In case of passing a function it gets a SubDataFrame
as its argument.
Here is an example:
combine(groupby(x, :id)) do sdf
n = nrow(sdf)
n < 25 ? DataFrame() : DataFrame(n=n) ## drop groups with low number of rows
end
Row | id | n |
---|---|---|
Char | Int64 | |
1 | d | 27 |
2 | c | 27 |
3 | b | 28 |
You can also produce multiple columns in a single operation:
df = DataFrame(id=[1, 1, 2, 2], val=[1, 2, 3, 4])
Row | id | val |
---|---|---|
Int64 | Int64 | |
1 | 1 | 1 |
2 | 1 | 2 |
3 | 2 | 3 |
4 | 2 | 4 |
@chain df begin
groupby(:id)
combine(:val => (x -> [x]) => AsTable)
end
Row | id | x1 | x2 |
---|---|---|---|
Int64 | Int64 | Int64 | |
1 | 1 | 1 | 2 |
2 | 2 | 3 | 4 |
@chain df begin
groupby(:id)
combine(:val => (x -> [x]) => [:c1, :c2])
end
Row | id | c1 | c2 |
---|---|---|---|
Int64 | Int64 | Int64 | |
1 | 1 | 1 | 2 |
2 | 2 | 3 | 4 |
It is easy to unnest the column into multiple columns,
df = DataFrame(a=[(p=1, q=2), (p=3, q=4)])
select(df, :a => AsTable)
Row | p | q |
---|---|---|
Int64 | Int64 | |
1 | 1 | 2 |
2 | 3 | 4 |
automatic column names generated
df = DataFrame(a=[[1, 2], [3, 4]])
select(df, :a => AsTable)
Row | x1 | x2 |
---|---|---|
Int64 | Int64 | |
1 | 1 | 2 |
2 | 3 | 4 |
custom column names generated
select(df, :a => [:C1, :C2])
Row | C1 | C2 |
---|---|---|
Int64 | Int64 | |
1 | 1 | 2 |
2 | 3 | 4 |
Finally, observe that one can conveniently apply multiple transformations using broadcasting:
df = DataFrame(id=repeat(1:10, 10), x1=1:100, x2=101:200)
Row | id | x1 | x2 |
---|---|---|---|
Int64 | Int64 | Int64 | |
1 | 1 | 1 | 101 |
2 | 2 | 2 | 102 |
3 | 3 | 3 | 103 |
4 | 4 | 4 | 104 |
5 | 5 | 5 | 105 |
6 | 6 | 6 | 106 |
7 | 7 | 7 | 107 |
8 | 8 | 8 | 108 |
9 | 9 | 9 | 109 |
10 | 10 | 10 | 110 |
11 | 1 | 11 | 111 |
12 | 2 | 12 | 112 |
13 | 3 | 13 | 113 |
⋮ | ⋮ | ⋮ | ⋮ |
89 | 9 | 89 | 189 |
90 | 10 | 90 | 190 |
91 | 1 | 91 | 191 |
92 | 2 | 92 | 192 |
93 | 3 | 93 | 193 |
94 | 4 | 94 | 194 |
95 | 5 | 95 | 195 |
96 | 6 | 96 | 196 |
97 | 7 | 97 | 197 |
98 | 8 | 98 | 198 |
99 | 9 | 99 | 199 |
100 | 10 | 100 | 200 |
@chain df begin
groupby(:id)
combine([:x1, :x2] .=> minimum)
end
Row | id | x1_minimum | x2_minimum |
---|---|---|---|
Int64 | Int64 | Int64 | |
1 | 1 | 1 | 101 |
2 | 2 | 2 | 102 |
3 | 3 | 3 | 103 |
4 | 4 | 4 | 104 |
5 | 5 | 5 | 105 |
6 | 6 | 6 | 106 |
7 | 7 | 7 | 107 |
8 | 8 | 8 | 108 |
9 | 9 | 9 | 109 |
10 | 10 | 10 | 110 |
@chain df begin
groupby(:id)
combine([:x1, :x2] .=> [minimum maximum])
end
Row | id | x1_minimum | x2_minimum | x1_maximum | x2_maximum |
---|---|---|---|---|---|
Int64 | Int64 | Int64 | Int64 | Int64 | |
1 | 1 | 1 | 101 | 91 | 191 |
2 | 2 | 2 | 102 | 92 | 192 |
3 | 3 | 3 | 103 | 93 | 193 |
4 | 4 | 4 | 104 | 94 | 194 |
5 | 5 | 5 | 105 | 95 | 195 |
6 | 6 | 6 | 106 | 96 | 196 |
7 | 7 | 7 | 107 | 97 | 197 |
8 | 8 | 8 | 108 | 98 | 198 |
9 | 9 | 9 | 109 | 99 | 199 |
10 | 10 | 10 | 110 | 100 | 200 |
Aggregation of a data frame using mapcols#
x = DataFrame(rand(10, 10), :auto)
Row | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 |
---|---|---|---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.0497713 | 0.174038 | 0.335658 | 0.98617 | 0.818046 | 0.315802 | 0.176087 | 0.88693 | 0.331478 | 0.565418 |
2 | 0.469067 | 0.532094 | 0.522563 | 0.698294 | 0.684567 | 0.713736 | 0.236339 | 0.436262 | 0.438289 | 0.501549 |
3 | 0.960727 | 0.792708 | 0.219594 | 0.991814 | 0.853047 | 0.661315 | 0.227833 | 0.796672 | 0.830572 | 0.0412893 |
4 | 0.954378 | 0.464174 | 0.469449 | 0.207181 | 0.98027 | 0.811747 | 0.908094 | 0.187583 | 0.127347 | 0.11277 |
5 | 0.490458 | 0.356026 | 0.220018 | 0.672967 | 0.0462723 | 0.578973 | 0.838352 | 0.145225 | 0.718935 | 0.254138 |
6 | 0.226693 | 0.975931 | 0.82475 | 0.132941 | 0.486135 | 0.189352 | 0.305374 | 0.478535 | 0.457187 | 0.0475593 |
7 | 0.520552 | 0.0662263 | 0.154305 | 0.440129 | 0.508249 | 0.531972 | 0.604084 | 0.38234 | 0.665801 | 0.563294 |
8 | 0.881169 | 0.782526 | 0.13947 | 0.571747 | 0.240582 | 0.556918 | 0.981525 | 0.813337 | 0.07942 | 0.616259 |
9 | 0.615944 | 0.740941 | 0.155328 | 0.450876 | 0.731792 | 0.414014 | 0.995569 | 0.825418 | 0.808066 | 0.795116 |
10 | 0.171597 | 0.927998 | 0.945204 | 0.761721 | 0.000796149 | 0.700339 | 0.824226 | 0.786892 | 0.33651 | 0.839618 |
mapcols(mean, x)
Row | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 |
---|---|---|---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.534036 | 0.581266 | 0.398634 | 0.591384 | 0.534976 | 0.547417 | 0.609748 | 0.573919 | 0.479361 | 0.433701 |
Mapping rows and columns using eachcol and eachrow#
map a function over each column and return a vector
map(mean, eachcol(x))
10-element Vector{Float64}:
0.5340356166118051
0.581266221422634
0.39863395825014675
0.5913839384886548
0.5349757146385773
0.5474167357793596
0.6097484043642387
0.5739193910908156
0.47936052129134554
0.4337009355738717
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.5340356166118051
x2: 0.581266221422634
x3: 0.39863395825014675
x4: 0.5913839384886548
x5: 0.5349757146385773
x6: 0.5474167357793596
x7: 0.6097484043642387
x8: 0.5739193910908156
x9: 0.47936052129134554
x10: 0.4337009355738717
now the returned value is DataFrameRow which works as a NamedTuple but is a view to a parent DataFrame
map(r -> r.x1 / r.x2, eachrow(x))
10-element Vector{Float64}:
0.28598028361408456
0.8815489568706143
1.211955077276455
2.056076699409006
1.3775932036047553
0.23228344373783585
7.860203109455978
1.1260573845407145
0.8312995463574625
0.18491066455919594
it prints like a data frame, only the caption is different so that you know the type of the object
er = eachrow(x)
er.x1 ## you can access columns of a parent data frame directly
10-element Vector{Float64}:
0.04977131658967582
0.4690673288817153
0.9607270015546927
0.9543781600816001
0.49045845644444097
0.22669264058140504
0.5205522834249062
0.8811686488976916
0.6159435670365279
0.17159676262539625
it prints like a data frame, only the caption is different so that you know the type of the object
ec = eachcol(x)
Row | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 |
---|---|---|---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.0497713 | 0.174038 | 0.335658 | 0.98617 | 0.818046 | 0.315802 | 0.176087 | 0.88693 | 0.331478 | 0.565418 |
2 | 0.469067 | 0.532094 | 0.522563 | 0.698294 | 0.684567 | 0.713736 | 0.236339 | 0.436262 | 0.438289 | 0.501549 |
3 | 0.960727 | 0.792708 | 0.219594 | 0.991814 | 0.853047 | 0.661315 | 0.227833 | 0.796672 | 0.830572 | 0.0412893 |
4 | 0.954378 | 0.464174 | 0.469449 | 0.207181 | 0.98027 | 0.811747 | 0.908094 | 0.187583 | 0.127347 | 0.11277 |
5 | 0.490458 | 0.356026 | 0.220018 | 0.672967 | 0.0462723 | 0.578973 | 0.838352 | 0.145225 | 0.718935 | 0.254138 |
6 | 0.226693 | 0.975931 | 0.82475 | 0.132941 | 0.486135 | 0.189352 | 0.305374 | 0.478535 | 0.457187 | 0.0475593 |
7 | 0.520552 | 0.0662263 | 0.154305 | 0.440129 | 0.508249 | 0.531972 | 0.604084 | 0.38234 | 0.665801 | 0.563294 |
8 | 0.881169 | 0.782526 | 0.13947 | 0.571747 | 0.240582 | 0.556918 | 0.981525 | 0.813337 | 0.07942 | 0.616259 |
9 | 0.615944 | 0.740941 | 0.155328 | 0.450876 | 0.731792 | 0.414014 | 0.995569 | 0.825418 | 0.808066 | 0.795116 |
10 | 0.171597 | 0.927998 | 0.945204 | 0.761721 | 0.000796149 | 0.700339 | 0.824226 | 0.786892 | 0.33651 | 0.839618 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.04977131658967582
0.4690673288817153
0.9607270015546927
0.9543781600816001
0.49045845644444097
0.22669264058140504
0.5205522834249062
0.8811686488976916
0.6159435670365279
0.17159676262539625
Transposing#
you can transpose a data frame using permutedims
:
df = DataFrame(reshape(1:12, 3, 4), :auto)
Row | x1 | x2 | x3 | x4 |
---|---|---|---|---|
Int64 | Int64 | Int64 | Int64 | |
1 | 1 | 4 | 7 | 10 |
2 | 2 | 5 | 8 | 11 |
3 | 3 | 6 | 9 | 12 |
df.names = ["a", "b", "c"]
3-element Vector{String}:
"a"
"b"
"c"
permutedims(df, :names)
Row | names | a | b | c |
---|---|---|---|---|
String | Int64 | Int64 | Int64 | |
1 | x1 | 1 | 2 | 3 |
2 | x2 | 4 | 5 | 6 |
3 | x3 | 7 | 8 | 9 |
4 | x4 | 10 | 11 | 12 |
This notebook was generated using Literate.jl.