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.339102 |
| 2 | 2 | 2 | 0.76979 |
| 3 | 3 | 1 | 0.743467 |
| 4 | 4 | 2 | 0.179302 |
| 5 | 1 | 1 | 0.0578305 |
| 6 | 2 | 2 | 0.043014 |
| 7 | 3 | 1 | 0.126171 |
| 8 | 4 | 2 | 0.00631068 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.339102 |
| 2 | 1 | 1 | 0.0578305 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.179302 |
| 2 | 4 | 2 | 0.00631068 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.339102 |
| 2 | 2 | 2 | 0.76979 |
| 3 | 3 | 1 | 0.743467 |
| 4 | 4 | 2 | 0.179302 |
| 5 | 1 | 1 | 0.0578305 |
| 6 | 2 | 2 | 0.043014 |
| 7 | 3 | 1 | 0.126171 |
| 8 | 4 | 2 | 0.00631068 |
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.339102 |
| 2 | 1 | 1 | 0.0578305 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.179302 |
| 2 | 4 | 2 | 0.00631068 |
get the parent DataFrame
parent(gx2)
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.339102 |
| 2 | 2 | 2 | 0.76979 |
| 3 | 3 | 1 | 0.743467 |
| 4 | 4 | 2 | 0.179302 |
| 5 | 1 | 1 | 0.0578305 |
| 6 | 2 | 2 | 0.043014 |
| 7 | 3 | 1 | 0.126171 |
| 8 | 4 | 2 | 0.00631068 |
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.339102 |
| 2 | 1 | 1 | 0.0578305 |
| 3 | 2 | 2 | 0.76979 |
| 4 | 2 | 2 | 0.043014 |
| 5 | 3 | 1 | 0.743467 |
| 6 | 3 | 1 | 0.126171 |
| 7 | 4 | 2 | 0.179302 |
| 8 | 4 | 2 | 0.00631068 |
the same as above
DataFrame(gx2)
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 1 | 1 | 0.339102 |
| 2 | 1 | 1 | 0.0578305 |
| 3 | 2 | 2 | 0.76979 |
| 4 | 2 | 2 | 0.043014 |
| 5 | 3 | 1 | 0.743467 |
| 6 | 3 | 1 | 0.126171 |
| 7 | 4 | 2 | 0.179302 |
| 8 | 4 | 2 | 0.00631068 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
| Row | v |
|---|---|
| Float64 | |
| 1 | 0.339102 |
| 2 | 0.0578305 |
| 3 | 0.76979 |
| 4 | 0.043014 |
| 5 | 0.743467 |
| 6 | 0.126171 |
| 7 | 0.179302 |
| 8 | 0.00631068 |
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.339102 |
| 2 | 1 | 1 | 0.0578305 |
⋮
| Row | id | id2 | v |
|---|---|---|---|
| Int64 | Int64 | Float64 | |
| 1 | 4 | 2 | 0.179302 |
| 2 | 4 | 2 | 0.00631068 |
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.339102
2 │ 1 1 0.0578305, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.339102
2 │ 1 1 0.0578305, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.339102
2 │ 1 1 0.0578305, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼─────────────────────────
1 │ 1 1 0.339102
2 │ 1 1 0.0578305)
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.370334 |
| 2 | b | 0.993104 |
| 3 | d | 0.635 |
| 4 | c | 0.616631 |
| 5 | b | 0.35559 |
| 6 | c | 0.124394 |
| 7 | b | 0.511263 |
| 8 | d | 0.874141 |
| 9 | b | 0.933005 |
| 10 | d | 0.588118 |
| 11 | a | 0.12122 |
| 12 | b | 0.822107 |
| 13 | c | 0.58134 |
| ⋮ | ⋮ | ⋮ |
| 89 | d | 0.54355 |
| 90 | d | 0.113034 |
| 91 | b | 0.817364 |
| 92 | c | 0.0342757 |
| 93 | b | 0.657528 |
| 94 | a | 0.877121 |
| 95 | b | 0.203659 |
| 96 | b | 0.241867 |
| 97 | c | 0.945331 |
| 98 | a | 0.0922752 |
| 99 | a | 0.928773 |
| 100 | b | 0.258577 |
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.424412 |
| 2 | b | 0.492894 |
| 3 | d | 0.47484 |
| 4 | c | 0.517263 |
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.370334 | 1 | 0.424412 |
| 2 | b | 0.993104 | 2 | 0.492894 |
| 3 | d | 0.635 | 3 | 0.47484 |
| 4 | c | 0.616631 | 4 | 0.517263 |
| 5 | b | 0.35559 | 5 | 0.492894 |
| 6 | c | 0.124394 | 6 | 0.517263 |
| 7 | b | 0.511263 | 7 | 0.492894 |
| 8 | d | 0.874141 | 8 | 0.47484 |
| 9 | b | 0.933005 | 9 | 0.492894 |
| 10 | d | 0.588118 | 10 | 0.47484 |
| 11 | a | 0.12122 | 11 | 0.424412 |
| 12 | b | 0.822107 | 12 | 0.492894 |
| 13 | c | 0.58134 | 13 | 0.517263 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 89 | d | 0.54355 | 89 | 0.47484 |
| 90 | d | 0.113034 | 90 | 0.47484 |
| 91 | b | 0.817364 | 91 | 0.492894 |
| 92 | c | 0.0342757 | 92 | 0.517263 |
| 93 | b | 0.657528 | 93 | 0.492894 |
| 94 | a | 0.877121 | 94 | 0.424412 |
| 95 | b | 0.203659 | 95 | 0.492894 |
| 96 | b | 0.241867 | 96 | 0.492894 |
| 97 | c | 0.945331 | 97 | 0.517263 |
| 98 | a | 0.0922752 | 98 | 0.424412 |
| 99 | a | 0.928773 | 99 | 0.424412 |
| 100 | b | 0.258577 | 100 | 0.492894 |
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.424412 |
| 2 | a | 11 | 0.424412 |
| 3 | a | 17 | 0.424412 |
| 4 | a | 20 | 0.424412 |
| 5 | a | 22 | 0.424412 |
| 6 | a | 25 | 0.424412 |
| 7 | a | 30 | 0.424412 |
| 8 | a | 32 | 0.424412 |
| 9 | a | 41 | 0.424412 |
| 10 | a | 55 | 0.424412 |
| 11 | a | 61 | 0.424412 |
| 12 | a | 66 | 0.424412 |
| 13 | a | 67 | 0.424412 |
| ⋮ | ⋮ | ⋮ | ⋮ |
| 89 | c | 56 | 0.517263 |
| 90 | c | 57 | 0.517263 |
| 91 | c | 59 | 0.517263 |
| 92 | c | 62 | 0.517263 |
| 93 | c | 72 | 0.517263 |
| 94 | c | 74 | 0.517263 |
| 95 | c | 77 | 0.517263 |
| 96 | c | 79 | 0.517263 |
| 97 | c | 83 | 0.517263 |
| 98 | c | 84 | 0.517263 |
| 99 | c | 92 | 0.517263 |
| 100 | c | 97 | 0.517263 |
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.424412 |
| 2 | b | 0.492894 |
| 3 | d | 0.47484 |
| 4 | c | 0.517263 |
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.424412 | 8.91264 | 21 |
| 2 | b | 0.492894 | 14.7868 | 30 |
| 3 | d | 0.47484 | 11.3962 | 24 |
| 4 | c | 0.517263 | 12.9316 | 25 |
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 | b | 30 |
| 2 | c | 25 |
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.464199 | 0.197573 | 0.471682 | 0.598597 | 0.203353 | 0.39853 | 0.707139 | 0.633737 | 0.906073 | 0.948284 |
| 2 | 0.412674 | 0.251407 | 0.114538 | 0.146708 | 0.46244 | 0.0758774 | 0.553053 | 0.0886115 | 0.914786 | 0.998204 |
| 3 | 0.173466 | 0.724795 | 0.129264 | 0.0100064 | 0.79954 | 0.0753851 | 0.949451 | 0.26599 | 0.866778 | 0.264724 |
| 4 | 0.453579 | 0.071489 | 0.0425121 | 0.64335 | 0.264881 | 0.0722715 | 0.616745 | 0.736837 | 0.804739 | 0.948438 |
| 5 | 0.11875 | 0.106227 | 0.9622 | 0.504896 | 0.57059 | 0.644028 | 0.372035 | 0.872128 | 0.73128 | 0.510869 |
| 6 | 0.325553 | 0.13065 | 0.893474 | 0.651575 | 0.67349 | 0.585531 | 0.191917 | 0.502565 | 0.359287 | 0.922355 |
| 7 | 0.905637 | 0.499335 | 0.303188 | 0.268708 | 0.638603 | 0.118515 | 0.310534 | 0.291868 | 0.0122852 | 0.637567 |
| 8 | 0.102074 | 0.622027 | 0.0834084 | 0.500928 | 0.342573 | 0.492162 | 0.674333 | 0.813899 | 0.0632016 | 0.396585 |
| 9 | 0.248494 | 0.0175227 | 0.0689599 | 0.145855 | 0.429778 | 0.255679 | 0.162785 | 0.961914 | 0.438382 | 0.110069 |
| 10 | 0.75359 | 0.340263 | 0.826692 | 0.743853 | 0.30519 | 0.400359 | 0.208686 | 0.400674 | 0.996291 | 0.7375 |
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.395802 | 0.296129 | 0.389592 | 0.421448 | 0.469044 | 0.311834 | 0.474668 | 0.556822 | 0.60931 | 0.647459 |
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.3958017176190256
0.2961289269833874
0.38959191218230754
0.4214476688437806
0.4690437545883105
0.31183387085376063
0.4746679779896309
0.55682222654873
0.6093101882415054
0.6474594562251919
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.3958017176190256
x2: 0.2961289269833874
x3: 0.38959191218230754
x4: 0.4214476688437806
x5: 0.4690437545883105
x6: 0.31183387085376063
x7: 0.4746679779896309
x8: 0.55682222654873
x9: 0.6093101882415054
x10: 0.6474594562251919
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}:
2.3495068850231715
1.6414563044865957
0.23933124317182303
6.3447452579481505
1.1178820382382946
2.4917889463094918
1.8136875370352246
0.16409978501458122
14.181299738280472
2.214727543588411
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.4641990970776172
0.4126742463842531
0.1734660417075179
0.45357920179274114
0.11874965606061971
0.3255530246477467
0.9056369528299589
0.10207449347088948
0.24849417821930797
0.7535902839996041
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.464199 | 0.197573 | 0.471682 | 0.598597 | 0.203353 | 0.39853 | 0.707139 | 0.633737 | 0.906073 | 0.948284 |
| 2 | 0.412674 | 0.251407 | 0.114538 | 0.146708 | 0.46244 | 0.0758774 | 0.553053 | 0.0886115 | 0.914786 | 0.998204 |
| 3 | 0.173466 | 0.724795 | 0.129264 | 0.0100064 | 0.79954 | 0.0753851 | 0.949451 | 0.26599 | 0.866778 | 0.264724 |
| 4 | 0.453579 | 0.071489 | 0.0425121 | 0.64335 | 0.264881 | 0.0722715 | 0.616745 | 0.736837 | 0.804739 | 0.948438 |
| 5 | 0.11875 | 0.106227 | 0.9622 | 0.504896 | 0.57059 | 0.644028 | 0.372035 | 0.872128 | 0.73128 | 0.510869 |
| 6 | 0.325553 | 0.13065 | 0.893474 | 0.651575 | 0.67349 | 0.585531 | 0.191917 | 0.502565 | 0.359287 | 0.922355 |
| 7 | 0.905637 | 0.499335 | 0.303188 | 0.268708 | 0.638603 | 0.118515 | 0.310534 | 0.291868 | 0.0122852 | 0.637567 |
| 8 | 0.102074 | 0.622027 | 0.0834084 | 0.500928 | 0.342573 | 0.492162 | 0.674333 | 0.813899 | 0.0632016 | 0.396585 |
| 9 | 0.248494 | 0.0175227 | 0.0689599 | 0.145855 | 0.429778 | 0.255679 | 0.162785 | 0.961914 | 0.438382 | 0.110069 |
| 10 | 0.75359 | 0.340263 | 0.826692 | 0.743853 | 0.30519 | 0.400359 | 0.208686 | 0.400674 | 0.996291 | 0.7375 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.4641990970776172
0.4126742463842531
0.1734660417075179
0.45357920179274114
0.11874965606061971
0.3255530246477467
0.9056369528299589
0.10207449347088948
0.24849417821930797
0.7535902839996041
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.