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.923554 |
2 | 2 | 2 | 0.525914 |
3 | 3 | 1 | 0.611834 |
4 | 4 | 2 | 0.105175 |
5 | 1 | 1 | 0.926859 |
6 | 2 | 2 | 0.843111 |
7 | 3 | 1 | 0.173183 |
8 | 4 | 2 | 0.439764 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.923554 |
2 | 1 | 1 | 0.926859 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.105175 |
2 | 4 | 2 | 0.439764 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.923554 |
2 | 2 | 2 | 0.525914 |
3 | 3 | 1 | 0.611834 |
4 | 4 | 2 | 0.105175 |
5 | 1 | 1 | 0.926859 |
6 | 2 | 2 | 0.843111 |
7 | 3 | 1 | 0.173183 |
8 | 4 | 2 | 0.439764 |
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.923554 |
2 | 1 | 1 | 0.926859 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.105175 |
2 | 4 | 2 | 0.439764 |
get the parent DataFrame
parent(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.923554 |
2 | 2 | 2 | 0.525914 |
3 | 3 | 1 | 0.611834 |
4 | 4 | 2 | 0.105175 |
5 | 1 | 1 | 0.926859 |
6 | 2 | 2 | 0.843111 |
7 | 3 | 1 | 0.173183 |
8 | 4 | 2 | 0.439764 |
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.923554 |
2 | 1 | 1 | 0.926859 |
3 | 2 | 2 | 0.525914 |
4 | 2 | 2 | 0.843111 |
5 | 3 | 1 | 0.611834 |
6 | 3 | 1 | 0.173183 |
7 | 4 | 2 | 0.105175 |
8 | 4 | 2 | 0.439764 |
the same as above
DataFrame(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.923554 |
2 | 1 | 1 | 0.926859 |
3 | 2 | 2 | 0.525914 |
4 | 2 | 2 | 0.843111 |
5 | 3 | 1 | 0.611834 |
6 | 3 | 1 | 0.173183 |
7 | 4 | 2 | 0.105175 |
8 | 4 | 2 | 0.439764 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
Row | v |
---|---|
Float64 | |
1 | 0.923554 |
2 | 0.926859 |
3 | 0.525914 |
4 | 0.843111 |
5 | 0.611834 |
6 | 0.173183 |
7 | 0.105175 |
8 | 0.439764 |
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.923554 |
2 | 1 | 1 | 0.926859 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.105175 |
2 | 4 | 2 | 0.439764 |
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.923554
2 │ 1 1 0.926859, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.923554
2 │ 1 1 0.926859, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.923554
2 │ 1 1 0.926859, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.923554
2 │ 1 1 0.926859)
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.835577 |
2 | c | 0.281793 |
3 | a | 0.578997 |
4 | b | 0.0623499 |
5 | a | 0.627195 |
6 | d | 0.871266 |
7 | a | 0.0218157 |
8 | c | 0.404754 |
9 | d | 0.787053 |
10 | c | 0.498892 |
11 | b | 0.229484 |
12 | d | 0.315011 |
13 | a | 0.921946 |
⋮ | ⋮ | ⋮ |
89 | d | 0.067035 |
90 | a | 0.933599 |
91 | c | 0.849821 |
92 | c | 0.906701 |
93 | b | 0.736553 |
94 | b | 0.779055 |
95 | b | 0.985637 |
96 | a | 0.34354 |
97 | d | 0.935088 |
98 | c | 0.742881 |
99 | a | 0.932195 |
100 | a | 0.730541 |
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.585795 |
2 | c | 0.460639 |
3 | b | 0.528949 |
4 | d | 0.585117 |
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.835577 | 1 | 0.585795 |
2 | c | 0.281793 | 2 | 0.460639 |
3 | a | 0.578997 | 3 | 0.585795 |
4 | b | 0.0623499 | 4 | 0.528949 |
5 | a | 0.627195 | 5 | 0.585795 |
6 | d | 0.871266 | 6 | 0.585117 |
7 | a | 0.0218157 | 7 | 0.585795 |
8 | c | 0.404754 | 8 | 0.460639 |
9 | d | 0.787053 | 9 | 0.585117 |
10 | c | 0.498892 | 10 | 0.460639 |
11 | b | 0.229484 | 11 | 0.528949 |
12 | d | 0.315011 | 12 | 0.585117 |
13 | a | 0.921946 | 13 | 0.585795 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
89 | d | 0.067035 | 89 | 0.585117 |
90 | a | 0.933599 | 90 | 0.585795 |
91 | c | 0.849821 | 91 | 0.460639 |
92 | c | 0.906701 | 92 | 0.460639 |
93 | b | 0.736553 | 93 | 0.528949 |
94 | b | 0.779055 | 94 | 0.528949 |
95 | b | 0.985637 | 95 | 0.528949 |
96 | a | 0.34354 | 96 | 0.585795 |
97 | d | 0.935088 | 97 | 0.585117 |
98 | c | 0.742881 | 98 | 0.460639 |
99 | a | 0.932195 | 99 | 0.585795 |
100 | a | 0.730541 | 100 | 0.585795 |
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.585795 |
2 | a | 3 | 0.585795 |
3 | a | 5 | 0.585795 |
4 | a | 7 | 0.585795 |
5 | a | 13 | 0.585795 |
6 | a | 17 | 0.585795 |
7 | a | 19 | 0.585795 |
8 | a | 25 | 0.585795 |
9 | a | 26 | 0.585795 |
10 | a | 27 | 0.585795 |
11 | a | 28 | 0.585795 |
12 | a | 30 | 0.585795 |
13 | a | 37 | 0.585795 |
⋮ | ⋮ | ⋮ | ⋮ |
89 | d | 64 | 0.585117 |
90 | d | 65 | 0.585117 |
91 | d | 68 | 0.585117 |
92 | d | 71 | 0.585117 |
93 | d | 77 | 0.585117 |
94 | d | 78 | 0.585117 |
95 | d | 80 | 0.585117 |
96 | d | 85 | 0.585117 |
97 | d | 87 | 0.585117 |
98 | d | 88 | 0.585117 |
99 | d | 89 | 0.585117 |
100 | d | 97 | 0.585117 |
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.585795 |
2 | c | 0.460639 |
3 | b | 0.528949 |
4 | d | 0.585117 |
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.585795 | 16.4023 | 28 |
2 | c | 0.460639 | 8.29149 | 18 |
3 | b | 0.528949 | 11.6369 | 22 |
4 | d | 0.585117 | 18.7237 | 32 |
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 | a | 28 |
2 | d | 32 |
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.888222 | 0.0809605 | 0.403429 | 0.511608 | 0.965816 | 0.478898 | 0.371856 | 0.957915 | 0.129378 | 0.876398 |
2 | 0.969358 | 0.0217866 | 0.901886 | 0.410378 | 0.812917 | 0.255134 | 0.91058 | 0.0088501 | 0.888141 | 0.743186 |
3 | 0.0350118 | 0.277128 | 0.672571 | 0.911096 | 0.972657 | 0.783727 | 0.298356 | 0.187414 | 0.555125 | 0.681353 |
4 | 0.31299 | 0.141234 | 0.0983423 | 0.590939 | 0.0682573 | 0.384461 | 0.998603 | 0.704168 | 0.200136 | 0.379971 |
5 | 0.256835 | 0.458744 | 0.84923 | 0.470579 | 0.525386 | 0.128736 | 0.696099 | 0.192121 | 0.734684 | 0.397709 |
6 | 0.675231 | 0.226636 | 0.11204 | 0.046839 | 0.743986 | 0.415168 | 0.326053 | 0.612582 | 0.708217 | 0.456039 |
7 | 0.409624 | 0.719965 | 0.693778 | 0.713999 | 0.255868 | 0.941009 | 0.460104 | 0.137039 | 0.0450871 | 0.426524 |
8 | 0.361776 | 0.0872905 | 0.755017 | 0.97836 | 0.658533 | 0.376703 | 0.321328 | 0.0635676 | 0.317717 | 0.130425 |
9 | 0.502291 | 0.09431 | 0.807559 | 0.56433 | 0.588048 | 0.0412632 | 0.731746 | 0.158557 | 0.378116 | 0.406653 |
10 | 0.0238244 | 0.222155 | 0.844815 | 0.488589 | 0.74649 | 0.411805 | 0.262241 | 0.352572 | 0.0119243 | 0.1269 |
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.443516 | 0.233021 | 0.613867 | 0.568672 | 0.633796 | 0.42169 | 0.537696 | 0.337479 | 0.396853 | 0.462516 |
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.44351626991809645
0.23302107000340802
0.6138666895806199
0.5686716035619371
0.633795739464339
0.4216904417231671
0.5376964271412635
0.33747865010160033
0.3968525913053566
0.4625157129085385
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.44351626991809645
x2: 0.23302107000340802
x3: 0.6138666895806199
x4: 0.5686716035619371
x5: 0.633795739464339
x6: 0.4216904417231671
x7: 0.5376964271412635
x8: 0.33747865010160033
x9: 0.3968525913053566
x10: 0.4625157129085385
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}:
10.971054379628248
44.49338049460191
0.1263380337364685
2.2161041599136193
0.5598644975669603
2.9793662606153277
0.5689497869814226
4.144501187588225
5.325959217502414
0.10724220030834118
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.8882216562522717
0.9693580655050634
0.03501184143705083
0.31299002717386293
0.2568346884017747
0.6752310702251116
0.40962414133358827
0.36177556410656886
0.5022912541860961
0.02382439055957608
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.888222 | 0.0809605 | 0.403429 | 0.511608 | 0.965816 | 0.478898 | 0.371856 | 0.957915 | 0.129378 | 0.876398 |
2 | 0.969358 | 0.0217866 | 0.901886 | 0.410378 | 0.812917 | 0.255134 | 0.91058 | 0.0088501 | 0.888141 | 0.743186 |
3 | 0.0350118 | 0.277128 | 0.672571 | 0.911096 | 0.972657 | 0.783727 | 0.298356 | 0.187414 | 0.555125 | 0.681353 |
4 | 0.31299 | 0.141234 | 0.0983423 | 0.590939 | 0.0682573 | 0.384461 | 0.998603 | 0.704168 | 0.200136 | 0.379971 |
5 | 0.256835 | 0.458744 | 0.84923 | 0.470579 | 0.525386 | 0.128736 | 0.696099 | 0.192121 | 0.734684 | 0.397709 |
6 | 0.675231 | 0.226636 | 0.11204 | 0.046839 | 0.743986 | 0.415168 | 0.326053 | 0.612582 | 0.708217 | 0.456039 |
7 | 0.409624 | 0.719965 | 0.693778 | 0.713999 | 0.255868 | 0.941009 | 0.460104 | 0.137039 | 0.0450871 | 0.426524 |
8 | 0.361776 | 0.0872905 | 0.755017 | 0.97836 | 0.658533 | 0.376703 | 0.321328 | 0.0635676 | 0.317717 | 0.130425 |
9 | 0.502291 | 0.09431 | 0.807559 | 0.56433 | 0.588048 | 0.0412632 | 0.731746 | 0.158557 | 0.378116 | 0.406653 |
10 | 0.0238244 | 0.222155 | 0.844815 | 0.488589 | 0.74649 | 0.411805 | 0.262241 | 0.352572 | 0.0119243 | 0.1269 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.8882216562522717
0.9693580655050634
0.03501184143705083
0.31299002717386293
0.2568346884017747
0.6752310702251116
0.40962414133358827
0.36177556410656886
0.5022912541860961
0.02382439055957608
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.