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.213093 |
2 | 2 | 2 | 0.37718 |
3 | 3 | 1 | 0.654804 |
4 | 4 | 2 | 0.393161 |
5 | 1 | 1 | 0.531108 |
6 | 2 | 2 | 0.202536 |
7 | 3 | 1 | 0.33627 |
8 | 4 | 2 | 0.374237 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.213093 |
2 | 1 | 1 | 0.531108 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.393161 |
2 | 4 | 2 | 0.374237 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.213093 |
2 | 2 | 2 | 0.37718 |
3 | 3 | 1 | 0.654804 |
4 | 4 | 2 | 0.393161 |
5 | 1 | 1 | 0.531108 |
6 | 2 | 2 | 0.202536 |
7 | 3 | 1 | 0.33627 |
8 | 4 | 2 | 0.374237 |
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.213093 |
2 | 1 | 1 | 0.531108 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.393161 |
2 | 4 | 2 | 0.374237 |
get the parent DataFrame
parent(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.213093 |
2 | 2 | 2 | 0.37718 |
3 | 3 | 1 | 0.654804 |
4 | 4 | 2 | 0.393161 |
5 | 1 | 1 | 0.531108 |
6 | 2 | 2 | 0.202536 |
7 | 3 | 1 | 0.33627 |
8 | 4 | 2 | 0.374237 |
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.213093 |
2 | 1 | 1 | 0.531108 |
3 | 2 | 2 | 0.37718 |
4 | 2 | 2 | 0.202536 |
5 | 3 | 1 | 0.654804 |
6 | 3 | 1 | 0.33627 |
7 | 4 | 2 | 0.393161 |
8 | 4 | 2 | 0.374237 |
the same as above
DataFrame(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.213093 |
2 | 1 | 1 | 0.531108 |
3 | 2 | 2 | 0.37718 |
4 | 2 | 2 | 0.202536 |
5 | 3 | 1 | 0.654804 |
6 | 3 | 1 | 0.33627 |
7 | 4 | 2 | 0.393161 |
8 | 4 | 2 | 0.374237 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
Row | v |
---|---|
Float64 | |
1 | 0.213093 |
2 | 0.531108 |
3 | 0.37718 |
4 | 0.202536 |
5 | 0.654804 |
6 | 0.33627 |
7 | 0.393161 |
8 | 0.374237 |
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.213093 |
2 | 1 | 1 | 0.531108 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.393161 |
2 | 4 | 2 | 0.374237 |
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.213093
2 │ 1 1 0.531108, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.213093
2 │ 1 1 0.531108, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.213093
2 │ 1 1 0.531108, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.213093
2 │ 1 1 0.531108)
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 | a | 0.801883 |
2 | d | 0.952816 |
3 | d | 0.0163985 |
4 | d | 0.628305 |
5 | d | 0.200988 |
6 | c | 0.679717 |
7 | b | 0.406815 |
8 | d | 0.0890824 |
9 | d | 0.647735 |
10 | b | 0.743218 |
11 | a | 0.979649 |
12 | d | 0.307343 |
13 | c | 0.29788 |
⋮ | ⋮ | ⋮ |
89 | a | 0.127252 |
90 | c | 0.123745 |
91 | c | 0.460812 |
92 | b | 0.818189 |
93 | b | 0.138276 |
94 | b | 0.676854 |
95 | a | 0.587076 |
96 | d | 0.613215 |
97 | d | 0.609037 |
98 | b | 0.823303 |
99 | a | 0.0850082 |
100 | a | 0.0878467 |
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 | a | 0.497656 |
2 | d | 0.457387 |
3 | c | 0.425741 |
4 | b | 0.568237 |
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 | a | 0.801883 | 1 | 0.497656 |
2 | d | 0.952816 | 2 | 0.457387 |
3 | d | 0.0163985 | 3 | 0.457387 |
4 | d | 0.628305 | 4 | 0.457387 |
5 | d | 0.200988 | 5 | 0.457387 |
6 | c | 0.679717 | 6 | 0.425741 |
7 | b | 0.406815 | 7 | 0.568237 |
8 | d | 0.0890824 | 8 | 0.457387 |
9 | d | 0.647735 | 9 | 0.457387 |
10 | b | 0.743218 | 10 | 0.568237 |
11 | a | 0.979649 | 11 | 0.497656 |
12 | d | 0.307343 | 12 | 0.457387 |
13 | c | 0.29788 | 13 | 0.425741 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
89 | a | 0.127252 | 89 | 0.497656 |
90 | c | 0.123745 | 90 | 0.425741 |
91 | c | 0.460812 | 91 | 0.425741 |
92 | b | 0.818189 | 92 | 0.568237 |
93 | b | 0.138276 | 93 | 0.568237 |
94 | b | 0.676854 | 94 | 0.568237 |
95 | a | 0.587076 | 95 | 0.497656 |
96 | d | 0.613215 | 96 | 0.457387 |
97 | d | 0.609037 | 97 | 0.457387 |
98 | b | 0.823303 | 98 | 0.568237 |
99 | a | 0.0850082 | 99 | 0.497656 |
100 | a | 0.0878467 | 100 | 0.497656 |
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 | a | 1 | 0.497656 |
2 | a | 11 | 0.497656 |
3 | a | 15 | 0.497656 |
4 | a | 19 | 0.497656 |
5 | a | 25 | 0.497656 |
6 | a | 34 | 0.497656 |
7 | a | 40 | 0.497656 |
8 | a | 49 | 0.497656 |
9 | a | 53 | 0.497656 |
10 | a | 55 | 0.497656 |
11 | a | 59 | 0.497656 |
12 | a | 76 | 0.497656 |
13 | a | 77 | 0.497656 |
⋮ | ⋮ | ⋮ | ⋮ |
89 | b | 44 | 0.568237 |
90 | b | 46 | 0.568237 |
91 | b | 58 | 0.568237 |
92 | b | 61 | 0.568237 |
93 | b | 62 | 0.568237 |
94 | b | 66 | 0.568237 |
95 | b | 70 | 0.568237 |
96 | b | 73 | 0.568237 |
97 | b | 92 | 0.568237 |
98 | b | 93 | 0.568237 |
99 | b | 94 | 0.568237 |
100 | b | 98 | 0.568237 |
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 | a | 0.497656 |
2 | d | 0.457387 |
3 | c | 0.425741 |
4 | b | 0.568237 |
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 | a | 0.497656 | 10.4508 | 21 |
2 | d | 0.457387 | 14.6364 | 32 |
3 | c | 0.425741 | 11.0693 | 26 |
4 | b | 0.568237 | 11.933 | 21 |
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 | 32 |
2 | c | 26 |
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.543187 | 0.654971 | 0.564967 | 0.0193184 | 0.0655621 | 0.333422 | 0.146635 | 0.696758 | 0.133701 | 0.441637 |
2 | 0.0216368 | 0.221818 | 0.812418 | 0.919989 | 0.499002 | 0.273137 | 0.269978 | 0.294127 | 0.125862 | 0.83274 |
3 | 0.0858233 | 0.729442 | 0.966261 | 0.682663 | 0.476421 | 0.903126 | 0.379712 | 0.0558343 | 0.89487 | 0.280855 |
4 | 0.44383 | 0.698613 | 0.879449 | 0.415938 | 0.484192 | 0.419159 | 0.275473 | 0.0465855 | 0.42337 | 0.87747 |
5 | 0.070336 | 0.935493 | 0.30444 | 0.801397 | 0.80048 | 0.467063 | 0.778272 | 0.480827 | 0.258181 | 0.310151 |
6 | 0.675448 | 0.663605 | 0.194207 | 0.924949 | 0.439675 | 0.0335385 | 0.742594 | 0.211452 | 0.528305 | 0.995182 |
7 | 0.496498 | 0.945806 | 0.168843 | 0.177435 | 0.0214421 | 0.411133 | 0.908437 | 0.423562 | 0.444285 | 0.0575748 |
8 | 0.39182 | 0.746754 | 0.583123 | 0.526052 | 0.406152 | 0.34729 | 0.663282 | 0.535099 | 0.0683799 | 0.502595 |
9 | 0.689919 | 0.480597 | 0.780622 | 0.153747 | 0.160571 | 0.941022 | 0.472905 | 0.476583 | 0.0153624 | 0.671748 |
10 | 0.317521 | 0.31849 | 0.950929 | 0.153774 | 0.733582 | 0.294143 | 0.828636 | 0.825528 | 0.630997 | 0.797777 |
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.373602 | 0.639559 | 0.620526 | 0.477526 | 0.408708 | 0.442303 | 0.546592 | 0.404636 | 0.352331 | 0.576773 |
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.37360182782733153
0.6395588106842118
0.6205259472123151
0.4775262465128011
0.4087079638457814
0.4423033580345047
0.5465923243787321
0.40463569092326396
0.3523313871012412
0.5767728830531882
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.37360182782733153
x2: 0.6395588106842118
x3: 0.6205259472123151
x4: 0.4775262465128011
x5: 0.4087079638457814
x6: 0.4423033580345047
x7: 0.5465923243787321
x8: 0.40463569092326396
x9: 0.3523313871012412
x10: 0.5767728830531882
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.8293307869115067
0.0975428614803122
0.11765595862937013
0.6353016147079975
0.07518609472932634
1.0178462703245712
0.5249474255772854
0.5246970294853852
1.4355460069910808
0.9969559193720787
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.5431872051020343
0.02163675847737223
0.08582325596595097
0.4438299117560812
0.07033603762854568
0.6754479893590396
0.496498351529858
0.39181953559070604
0.6899186955395462
0.3175205373241805
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.543187 | 0.654971 | 0.564967 | 0.0193184 | 0.0655621 | 0.333422 | 0.146635 | 0.696758 | 0.133701 | 0.441637 |
2 | 0.0216368 | 0.221818 | 0.812418 | 0.919989 | 0.499002 | 0.273137 | 0.269978 | 0.294127 | 0.125862 | 0.83274 |
3 | 0.0858233 | 0.729442 | 0.966261 | 0.682663 | 0.476421 | 0.903126 | 0.379712 | 0.0558343 | 0.89487 | 0.280855 |
4 | 0.44383 | 0.698613 | 0.879449 | 0.415938 | 0.484192 | 0.419159 | 0.275473 | 0.0465855 | 0.42337 | 0.87747 |
5 | 0.070336 | 0.935493 | 0.30444 | 0.801397 | 0.80048 | 0.467063 | 0.778272 | 0.480827 | 0.258181 | 0.310151 |
6 | 0.675448 | 0.663605 | 0.194207 | 0.924949 | 0.439675 | 0.0335385 | 0.742594 | 0.211452 | 0.528305 | 0.995182 |
7 | 0.496498 | 0.945806 | 0.168843 | 0.177435 | 0.0214421 | 0.411133 | 0.908437 | 0.423562 | 0.444285 | 0.0575748 |
8 | 0.39182 | 0.746754 | 0.583123 | 0.526052 | 0.406152 | 0.34729 | 0.663282 | 0.535099 | 0.0683799 | 0.502595 |
9 | 0.689919 | 0.480597 | 0.780622 | 0.153747 | 0.160571 | 0.941022 | 0.472905 | 0.476583 | 0.0153624 | 0.671748 |
10 | 0.317521 | 0.31849 | 0.950929 | 0.153774 | 0.733582 | 0.294143 | 0.828636 | 0.825528 | 0.630997 | 0.797777 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.5431872051020343
0.02163675847737223
0.08582325596595097
0.4438299117560812
0.07033603762854568
0.6754479893590396
0.496498351529858
0.39181953559070604
0.6899186955395462
0.3175205373241805
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.