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.756392 |
| 2 | 2 | 2 | 0.405107 |
| 3 | 3 | 1 | 0.855122 |
| 4 | 4 | 2 | 0.989225 |
| 5 | 1 | 1 | 0.117164 |
| 6 | 2 | 2 | 0.3996 |
| 7 | 3 | 1 | 0.32268 |
| 8 | 4 | 2 | 0.317763 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.756392 |
| 2 | 1 | 1 | 0.117164 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.989225 |
| 2 | 4 | 2 | 0.317763 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.756392 |
| 2 | 2 | 2 | 0.405107 |
| 3 | 3 | 1 | 0.855122 |
| 4 | 4 | 2 | 0.989225 |
| 5 | 1 | 1 | 0.117164 |
| 6 | 2 | 2 | 0.3996 |
| 7 | 3 | 1 | 0.32268 |
| 8 | 4 | 2 | 0.317763 |
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.756392 |
| 2 | 1 | 1 | 0.117164 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.989225 |
| 2 | 4 | 2 | 0.317763 |
get the parent DataFrame
parent(gx2)
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.756392 |
| 2 | 2 | 2 | 0.405107 |
| 3 | 3 | 1 | 0.855122 |
| 4 | 4 | 2 | 0.989225 |
| 5 | 1 | 1 | 0.117164 |
| 6 | 2 | 2 | 0.3996 |
| 7 | 3 | 1 | 0.32268 |
| 8 | 4 | 2 | 0.317763 |
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.756392 |
| 2 | 1 | 1 | 0.117164 |
| 3 | 2 | 2 | 0.405107 |
| 4 | 2 | 2 | 0.3996 |
| 5 | 3 | 1 | 0.855122 |
| 6 | 3 | 1 | 0.32268 |
| 7 | 4 | 2 | 0.989225 |
| 8 | 4 | 2 | 0.317763 |
the same as above
DataFrame(gx2)
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.756392 |
| 2 | 1 | 1 | 0.117164 |
| 3 | 2 | 2 | 0.405107 |
| 4 | 2 | 2 | 0.3996 |
| 5 | 3 | 1 | 0.855122 |
| 6 | 3 | 1 | 0.32268 |
| 7 | 4 | 2 | 0.989225 |
| 8 | 4 | 2 | 0.317763 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
| Row | v |
|---|---|
| Float64 | |
| 1 | 0.756392 |
| 2 | 0.117164 |
| 3 | 0.405107 |
| 4 | 0.3996 |
| 5 | 0.855122 |
| 6 | 0.32268 |
| 7 | 0.989225 |
| 8 | 0.317763 |
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.756392 |
| 2 | 1 | 1 | 0.117164 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.989225 |
| 2 | 4 | 2 | 0.317763 |
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.756392
2 │ 1 1 0.117164, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.756392
2 │ 1 1 0.117164, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.756392
2 │ 1 1 0.117164, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.756392
2 │ 1 1 0.117164)
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 | c | 0.164321 |
| 2 | a | 0.928849 |
| 3 | c | 0.177093 |
| 4 | c | 0.465703 |
| 5 | d | 0.270039 |
| 6 | c | 0.0326164 |
| 7 | a | 0.914675 |
| 8 | c | 0.420337 |
| 9 | d | 0.746542 |
| 10 | c | 0.609532 |
| 11 | c | 0.389072 |
| 12 | b | 0.80542 |
| 13 | a | 0.588681 |
| ⋮ | ⋮ | ⋮ |
| 89 | d | 0.99002 |
| 90 | c | 0.855962 |
| 91 | c | 0.425813 |
| 92 | a | 0.412515 |
| 93 | b | 0.158071 |
| 94 | c | 0.183782 |
| 95 | c | 0.347485 |
| 96 | c | 0.812393 |
| 97 | d | 0.574843 |
| 98 | a | 0.1684 |
| 99 | b | 0.118532 |
| 100 | d | 0.165367 |
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 | c | 0.443746 |
| 2 | a | 0.568155 |
| 3 | d | 0.515793 |
| 4 | b | 0.558574 |
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 | c | 0.164321 | 1 | 0.443746 |
| 2 | a | 0.928849 | 2 | 0.568155 |
| 3 | c | 0.177093 | 3 | 0.443746 |
| 4 | c | 0.465703 | 4 | 0.443746 |
| 5 | d | 0.270039 | 5 | 0.515793 |
| 6 | c | 0.0326164 | 6 | 0.443746 |
| 7 | a | 0.914675 | 7 | 0.568155 |
| 8 | c | 0.420337 | 8 | 0.443746 |
| 9 | d | 0.746542 | 9 | 0.515793 |
| 10 | c | 0.609532 | 10 | 0.443746 |
| 11 | c | 0.389072 | 11 | 0.443746 |
| 12 | b | 0.80542 | 12 | 0.558574 |
| 13 | a | 0.588681 | 13 | 0.568155 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 89 | d | 0.99002 | 89 | 0.515793 |
| 90 | c | 0.855962 | 90 | 0.443746 |
| 91 | c | 0.425813 | 91 | 0.443746 |
| 92 | a | 0.412515 | 92 | 0.568155 |
| 93 | b | 0.158071 | 93 | 0.558574 |
| 94 | c | 0.183782 | 94 | 0.443746 |
| 95 | c | 0.347485 | 95 | 0.443746 |
| 96 | c | 0.812393 | 96 | 0.443746 |
| 97 | d | 0.574843 | 97 | 0.515793 |
| 98 | a | 0.1684 | 98 | 0.568155 |
| 99 | b | 0.118532 | 99 | 0.558574 |
| 100 | d | 0.165367 | 100 | 0.515793 |
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 | c | 1 | 0.443746 |
| 2 | c | 3 | 0.443746 |
| 3 | c | 4 | 0.443746 |
| 4 | c | 6 | 0.443746 |
| 5 | c | 8 | 0.443746 |
| 6 | c | 10 | 0.443746 |
| 7 | c | 11 | 0.443746 |
| 8 | c | 19 | 0.443746 |
| 9 | c | 26 | 0.443746 |
| 10 | c | 30 | 0.443746 |
| 11 | c | 31 | 0.443746 |
| 12 | c | 34 | 0.443746 |
| 13 | c | 37 | 0.443746 |
| ⋮ | ⋮ | ⋮ | ⋮ |
| 89 | b | 14 | 0.558574 |
| 90 | b | 21 | 0.558574 |
| 91 | b | 27 | 0.558574 |
| 92 | b | 33 | 0.558574 |
| 93 | b | 36 | 0.558574 |
| 94 | b | 40 | 0.558574 |
| 95 | b | 66 | 0.558574 |
| 96 | b | 68 | 0.558574 |
| 97 | b | 72 | 0.558574 |
| 98 | b | 76 | 0.558574 |
| 99 | b | 93 | 0.558574 |
| 100 | b | 99 | 0.558574 |
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 | c | 0.443746 |
| 2 | a | 0.568155 |
| 3 | d | 0.515793 |
| 4 | b | 0.558574 |
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 | c | 0.443746 | 16.4186 | 37 |
| 2 | a | 0.568155 | 16.4765 | 29 |
| 3 | d | 0.515793 | 10.8316 | 21 |
| 4 | b | 0.558574 | 7.26146 | 13 |
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 | c | 37 |
| 2 | a | 29 |
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.428413 | 0.288273 | 0.380249 | 0.0962524 | 0.87573 | 0.892209 | 0.341893 | 0.967807 | 0.0425122 | 0.398241 |
| 2 | 0.641005 | 0.828817 | 0.292158 | 0.648656 | 0.561629 | 0.176464 | 0.680028 | 0.472814 | 0.0623693 | 0.355808 |
| 3 | 0.246123 | 0.535485 | 0.501676 | 0.767878 | 0.158613 | 0.218819 | 0.517465 | 0.55468 | 0.848898 | 0.508709 |
| 4 | 0.481773 | 0.883589 | 0.484177 | 0.976891 | 0.953131 | 0.131042 | 0.34662 | 0.42991 | 0.989193 | 0.0319736 |
| 5 | 0.72787 | 0.734414 | 0.277308 | 0.65552 | 0.297051 | 0.184805 | 0.81433 | 0.357484 | 0.240209 | 0.132601 |
| 6 | 0.246995 | 0.498433 | 0.158371 | 0.516696 | 0.735091 | 0.629004 | 0.812174 | 0.514785 | 0.0429962 | 0.0967131 |
| 7 | 0.795772 | 0.889987 | 0.913867 | 0.402521 | 0.625666 | 0.279997 | 0.337937 | 0.79828 | 0.0840332 | 0.227062 |
| 8 | 0.533317 | 0.437208 | 0.361738 | 0.610043 | 0.885454 | 0.842465 | 0.22449 | 0.570867 | 0.674115 | 0.695531 |
| 9 | 0.761087 | 0.04645 | 0.717471 | 0.849107 | 0.999313 | 0.201569 | 0.344265 | 0.883335 | 0.994071 | 0.364646 |
| 10 | 0.69455 | 0.0322291 | 0.0858867 | 0.161196 | 0.475494 | 0.608615 | 0.894533 | 0.895358 | 0.579155 | 0.0914591 |
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.55569 | 0.517489 | 0.41729 | 0.568476 | 0.656717 | 0.416499 | 0.531373 | 0.644532 | 0.455755 | 0.290274 |
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.5556904112031291
0.5174885931860757
0.4172901102513499
0.5684760119865997
0.6567171643899633
0.41649895612771
0.5313734330242246
0.6445319618589554
0.4557552139097991
0.29027441029936235
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.5556904112031291
x2: 0.5174885931860757
x3: 0.4172901102513499
x4: 0.5684760119865997
x5: 0.6567171643899633
x6: 0.41649895612771
x7: 0.5313734330242246
x8: 0.6445319618589554
x9: 0.4557552139097991
x10: 0.29027441029936235
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}:
1.4861381928287993
0.7733965185752516
0.45962608488578244
0.5452456518417225
0.9910894748966226
0.49554198049022985
0.8941381695084383
1.2198258072343713
16.385072165899587
21.550374040782003
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.42841287488654944
0.6410045042984526
0.24612297675874162
0.48177314942789873
0.7278698016799588
0.24699461528955446
0.7957717820207632
0.533317366688105
0.7610873308098037
0.6945497101714644
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.428413 | 0.288273 | 0.380249 | 0.0962524 | 0.87573 | 0.892209 | 0.341893 | 0.967807 | 0.0425122 | 0.398241 |
| 2 | 0.641005 | 0.828817 | 0.292158 | 0.648656 | 0.561629 | 0.176464 | 0.680028 | 0.472814 | 0.0623693 | 0.355808 |
| 3 | 0.246123 | 0.535485 | 0.501676 | 0.767878 | 0.158613 | 0.218819 | 0.517465 | 0.55468 | 0.848898 | 0.508709 |
| 4 | 0.481773 | 0.883589 | 0.484177 | 0.976891 | 0.953131 | 0.131042 | 0.34662 | 0.42991 | 0.989193 | 0.0319736 |
| 5 | 0.72787 | 0.734414 | 0.277308 | 0.65552 | 0.297051 | 0.184805 | 0.81433 | 0.357484 | 0.240209 | 0.132601 |
| 6 | 0.246995 | 0.498433 | 0.158371 | 0.516696 | 0.735091 | 0.629004 | 0.812174 | 0.514785 | 0.0429962 | 0.0967131 |
| 7 | 0.795772 | 0.889987 | 0.913867 | 0.402521 | 0.625666 | 0.279997 | 0.337937 | 0.79828 | 0.0840332 | 0.227062 |
| 8 | 0.533317 | 0.437208 | 0.361738 | 0.610043 | 0.885454 | 0.842465 | 0.22449 | 0.570867 | 0.674115 | 0.695531 |
| 9 | 0.761087 | 0.04645 | 0.717471 | 0.849107 | 0.999313 | 0.201569 | 0.344265 | 0.883335 | 0.994071 | 0.364646 |
| 10 | 0.69455 | 0.0322291 | 0.0858867 | 0.161196 | 0.475494 | 0.608615 | 0.894533 | 0.895358 | 0.579155 | 0.0914591 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.42841287488654944
0.6410045042984526
0.24612297675874162
0.48177314942789873
0.7278698016799588
0.24699461528955446
0.7957717820207632
0.533317366688105
0.7610873308098037
0.6945497101714644
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.