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.668046 |
| 2 | 2 | 2 | 0.0217206 |
| 3 | 3 | 1 | 0.825651 |
| 4 | 4 | 2 | 0.835345 |
| 5 | 1 | 1 | 0.0516351 |
| 6 | 2 | 2 | 0.91682 |
| 7 | 3 | 1 | 0.669437 |
| 8 | 4 | 2 | 0.524971 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.668046 |
| 2 | 1 | 1 | 0.0516351 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.835345 |
| 2 | 4 | 2 | 0.524971 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.668046 |
| 2 | 2 | 2 | 0.0217206 |
| 3 | 3 | 1 | 0.825651 |
| 4 | 4 | 2 | 0.835345 |
| 5 | 1 | 1 | 0.0516351 |
| 6 | 2 | 2 | 0.91682 |
| 7 | 3 | 1 | 0.669437 |
| 8 | 4 | 2 | 0.524971 |
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.668046 |
| 2 | 1 | 1 | 0.0516351 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.835345 |
| 2 | 4 | 2 | 0.524971 |
get the parent DataFrame
parent(gx2)
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.668046 |
| 2 | 2 | 2 | 0.0217206 |
| 3 | 3 | 1 | 0.825651 |
| 4 | 4 | 2 | 0.835345 |
| 5 | 1 | 1 | 0.0516351 |
| 6 | 2 | 2 | 0.91682 |
| 7 | 3 | 1 | 0.669437 |
| 8 | 4 | 2 | 0.524971 |
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.668046 |
| 2 | 1 | 1 | 0.0516351 |
| 3 | 2 | 2 | 0.0217206 |
| 4 | 2 | 2 | 0.91682 |
| 5 | 3 | 1 | 0.825651 |
| 6 | 3 | 1 | 0.669437 |
| 7 | 4 | 2 | 0.835345 |
| 8 | 4 | 2 | 0.524971 |
the same as above
DataFrame(gx2)
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.668046 |
| 2 | 1 | 1 | 0.0516351 |
| 3 | 2 | 2 | 0.0217206 |
| 4 | 2 | 2 | 0.91682 |
| 5 | 3 | 1 | 0.825651 |
| 6 | 3 | 1 | 0.669437 |
| 7 | 4 | 2 | 0.835345 |
| 8 | 4 | 2 | 0.524971 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
| Row | v |
|---|---|
| Float64 | |
| 1 | 0.668046 |
| 2 | 0.0516351 |
| 3 | 0.0217206 |
| 4 | 0.91682 |
| 5 | 0.825651 |
| 6 | 0.669437 |
| 7 | 0.835345 |
| 8 | 0.524971 |
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.668046 |
| 2 | 1 | 1 | 0.0516351 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.835345 |
| 2 | 4 | 2 | 0.524971 |
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.668046
2 │ 1 1 0.0516351, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.668046
2 │ 1 1 0.0516351, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.668046
2 │ 1 1 0.0516351, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.668046
2 │ 1 1 0.0516351)
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.170523 |
| 2 | d | 0.914176 |
| 3 | d | 0.204357 |
| 4 | d | 0.603191 |
| 5 | b | 0.887813 |
| 6 | b | 0.414486 |
| 7 | c | 0.0844315 |
| 8 | d | 0.0611222 |
| 9 | a | 0.960798 |
| 10 | b | 0.427352 |
| 11 | d | 0.527319 |
| 12 | a | 0.763995 |
| 13 | a | 0.0469857 |
| ⋮ | ⋮ | ⋮ |
| 89 | d | 0.15171 |
| 90 | b | 0.461184 |
| 91 | b | 0.583866 |
| 92 | c | 0.658358 |
| 93 | c | 0.24761 |
| 94 | c | 0.590359 |
| 95 | b | 0.432363 |
| 96 | a | 0.844712 |
| 97 | b | 0.825326 |
| 98 | a | 0.210774 |
| 99 | a | 0.0527467 |
| 100 | c | 0.985588 |
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.481783 |
| 2 | d | 0.534384 |
| 3 | b | 0.583599 |
| 4 | c | 0.45104 |
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.170523 | 1 | 0.481783 |
| 2 | d | 0.914176 | 2 | 0.534384 |
| 3 | d | 0.204357 | 3 | 0.534384 |
| 4 | d | 0.603191 | 4 | 0.534384 |
| 5 | b | 0.887813 | 5 | 0.583599 |
| 6 | b | 0.414486 | 6 | 0.583599 |
| 7 | c | 0.0844315 | 7 | 0.45104 |
| 8 | d | 0.0611222 | 8 | 0.534384 |
| 9 | a | 0.960798 | 9 | 0.481783 |
| 10 | b | 0.427352 | 10 | 0.583599 |
| 11 | d | 0.527319 | 11 | 0.534384 |
| 12 | a | 0.763995 | 12 | 0.481783 |
| 13 | a | 0.0469857 | 13 | 0.481783 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 89 | d | 0.15171 | 89 | 0.534384 |
| 90 | b | 0.461184 | 90 | 0.583599 |
| 91 | b | 0.583866 | 91 | 0.583599 |
| 92 | c | 0.658358 | 92 | 0.45104 |
| 93 | c | 0.24761 | 93 | 0.45104 |
| 94 | c | 0.590359 | 94 | 0.45104 |
| 95 | b | 0.432363 | 95 | 0.583599 |
| 96 | a | 0.844712 | 96 | 0.481783 |
| 97 | b | 0.825326 | 97 | 0.583599 |
| 98 | a | 0.210774 | 98 | 0.481783 |
| 99 | a | 0.0527467 | 99 | 0.481783 |
| 100 | c | 0.985588 | 100 | 0.45104 |
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.481783 |
| 2 | a | 9 | 0.481783 |
| 3 | a | 12 | 0.481783 |
| 4 | a | 13 | 0.481783 |
| 5 | a | 14 | 0.481783 |
| 6 | a | 16 | 0.481783 |
| 7 | a | 18 | 0.481783 |
| 8 | a | 23 | 0.481783 |
| 9 | a | 24 | 0.481783 |
| 10 | a | 28 | 0.481783 |
| 11 | a | 33 | 0.481783 |
| 12 | a | 34 | 0.481783 |
| 13 | a | 38 | 0.481783 |
| ⋮ | ⋮ | ⋮ | ⋮ |
| 89 | c | 44 | 0.45104 |
| 90 | c | 45 | 0.45104 |
| 91 | c | 60 | 0.45104 |
| 92 | c | 61 | 0.45104 |
| 93 | c | 66 | 0.45104 |
| 94 | c | 68 | 0.45104 |
| 95 | c | 77 | 0.45104 |
| 96 | c | 80 | 0.45104 |
| 97 | c | 92 | 0.45104 |
| 98 | c | 93 | 0.45104 |
| 99 | c | 94 | 0.45104 |
| 100 | c | 100 | 0.45104 |
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.481783 |
| 2 | d | 0.534384 |
| 3 | b | 0.583599 |
| 4 | c | 0.45104 |
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.481783 | 13.9717 | 29 |
| 2 | d | 0.534384 | 13.3596 | 25 |
| 3 | b | 0.583599 | 16.3408 | 28 |
| 4 | c | 0.45104 | 8.11873 | 18 |
Additional notes:
select!andtransform!perform operations in-placeThe general syntax for transformation is
source_columns => function => target_columnif you pass multiple columns to a function they are treated as positional arguments
ByRowandAsTablework exactly like discussed for operations on data frames in 05_columns.ipynbyou can automatically groupby again the result of
combine,selectetc. by passingungroup=falsekeyword argument to themsimilarly
keepkeyskeyword 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 | 29 |
| 2 | d | 25 |
| 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.868459 | 0.00654418 | 0.433637 | 0.930547 | 0.23168 | 0.42777 | 0.7547 | 0.520275 | 0.425056 | 0.861338 |
| 2 | 0.144681 | 0.0194137 | 0.111178 | 0.659497 | 0.524944 | 0.0739465 | 0.598811 | 0.128128 | 0.261384 | 0.512681 |
| 3 | 0.973916 | 0.481915 | 0.800024 | 0.0407536 | 0.629961 | 0.913566 | 0.110335 | 0.72225 | 0.768512 | 0.692188 |
| 4 | 0.341584 | 0.312496 | 0.0284095 | 0.67526 | 0.115266 | 0.92251 | 0.741232 | 0.502286 | 0.82653 | 0.354182 |
| 5 | 0.387646 | 0.670977 | 0.325491 | 0.173094 | 0.2506 | 0.990914 | 0.31726 | 0.28657 | 0.574434 | 0.132789 |
| 6 | 0.717522 | 0.906963 | 0.447598 | 0.265681 | 0.164826 | 0.136781 | 0.66608 | 0.477339 | 0.665717 | 0.995873 |
| 7 | 0.669004 | 0.763295 | 0.686524 | 0.13838 | 0.100231 | 0.993652 | 0.578947 | 0.423681 | 0.160622 | 0.959578 |
| 8 | 0.573946 | 0.265492 | 0.312578 | 0.417322 | 0.82173 | 0.438493 | 0.0523987 | 0.331511 | 0.519 | 0.159041 |
| 9 | 0.546238 | 0.100002 | 0.426146 | 0.17062 | 0.0232923 | 0.321438 | 0.150506 | 0.674669 | 0.544913 | 0.614595 |
| 10 | 0.74745 | 0.192785 | 0.261677 | 0.081463 | 0.86766 | 0.0788269 | 0.463088 | 0.340394 | 0.728178 | 0.672423 |
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.597045 | 0.371988 | 0.383326 | 0.355262 | 0.373019 | 0.52979 | 0.443336 | 0.44071 | 0.547435 | 0.595469 |
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.5970446200515614
0.371988280043345
0.3833261421741702
0.35526177851212193
0.3730189771544299
0.5297896374308705
0.4433358543846618
0.4407101912829486
0.5474346036000817
0.5954687362554464
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.5970446200515614
x2: 0.371988280043345
x3: 0.3833261421741702
x4: 0.35526177851212193
x5: 0.3730189771544299
x6: 0.5297896374308705
x7: 0.4433358543846618
x8: 0.4407101912829486
x9: 0.5474346036000817
x10: 0.5954687362554464
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}:
132.70712754424997
7.452529393425048
2.0209313586052504
1.0930815434814145
0.5777342899285142
0.7911259171572509
0.8764693136551583
2.161819129709327
5.4622427942466985
3.8771071571962517
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.8684590156304837
0.144681492763982
0.9739162879177052
0.341583618406165
0.38764646475147546
0.7175215916255719
0.6690043125147219
0.5739460726996791
0.5462377762078239
0.7474495679980068
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.868459 | 0.00654418 | 0.433637 | 0.930547 | 0.23168 | 0.42777 | 0.7547 | 0.520275 | 0.425056 | 0.861338 |
| 2 | 0.144681 | 0.0194137 | 0.111178 | 0.659497 | 0.524944 | 0.0739465 | 0.598811 | 0.128128 | 0.261384 | 0.512681 |
| 3 | 0.973916 | 0.481915 | 0.800024 | 0.0407536 | 0.629961 | 0.913566 | 0.110335 | 0.72225 | 0.768512 | 0.692188 |
| 4 | 0.341584 | 0.312496 | 0.0284095 | 0.67526 | 0.115266 | 0.92251 | 0.741232 | 0.502286 | 0.82653 | 0.354182 |
| 5 | 0.387646 | 0.670977 | 0.325491 | 0.173094 | 0.2506 | 0.990914 | 0.31726 | 0.28657 | 0.574434 | 0.132789 |
| 6 | 0.717522 | 0.906963 | 0.447598 | 0.265681 | 0.164826 | 0.136781 | 0.66608 | 0.477339 | 0.665717 | 0.995873 |
| 7 | 0.669004 | 0.763295 | 0.686524 | 0.13838 | 0.100231 | 0.993652 | 0.578947 | 0.423681 | 0.160622 | 0.959578 |
| 8 | 0.573946 | 0.265492 | 0.312578 | 0.417322 | 0.82173 | 0.438493 | 0.0523987 | 0.331511 | 0.519 | 0.159041 |
| 9 | 0.546238 | 0.100002 | 0.426146 | 0.17062 | 0.0232923 | 0.321438 | 0.150506 | 0.674669 | 0.544913 | 0.614595 |
| 10 | 0.74745 | 0.192785 | 0.261677 | 0.081463 | 0.86766 | 0.0788269 | 0.463088 | 0.340394 | 0.728178 | 0.672423 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.8684590156304837
0.144681492763982
0.9739162879177052
0.341583618406165
0.38764646475147546
0.7175215916255719
0.6690043125147219
0.5739460726996791
0.5462377762078239
0.7474495679980068
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.