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.452854 |
2 | 2 | 2 | 0.604998 |
3 | 3 | 1 | 0.589787 |
4 | 4 | 2 | 0.224989 |
5 | 1 | 1 | 0.259274 |
6 | 2 | 2 | 0.663381 |
7 | 3 | 1 | 0.637711 |
8 | 4 | 2 | 0.0547333 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.452854 |
2 | 1 | 1 | 0.259274 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.224989 |
2 | 4 | 2 | 0.0547333 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.452854 |
2 | 2 | 2 | 0.604998 |
3 | 3 | 1 | 0.589787 |
4 | 4 | 2 | 0.224989 |
5 | 1 | 1 | 0.259274 |
6 | 2 | 2 | 0.663381 |
7 | 3 | 1 | 0.637711 |
8 | 4 | 2 | 0.0547333 |
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.452854 |
2 | 1 | 1 | 0.259274 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.224989 |
2 | 4 | 2 | 0.0547333 |
get the parent DataFrame
parent(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.452854 |
2 | 2 | 2 | 0.604998 |
3 | 3 | 1 | 0.589787 |
4 | 4 | 2 | 0.224989 |
5 | 1 | 1 | 0.259274 |
6 | 2 | 2 | 0.663381 |
7 | 3 | 1 | 0.637711 |
8 | 4 | 2 | 0.0547333 |
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.452854 |
2 | 1 | 1 | 0.259274 |
3 | 2 | 2 | 0.604998 |
4 | 2 | 2 | 0.663381 |
5 | 3 | 1 | 0.589787 |
6 | 3 | 1 | 0.637711 |
7 | 4 | 2 | 0.224989 |
8 | 4 | 2 | 0.0547333 |
the same as above
DataFrame(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.452854 |
2 | 1 | 1 | 0.259274 |
3 | 2 | 2 | 0.604998 |
4 | 2 | 2 | 0.663381 |
5 | 3 | 1 | 0.589787 |
6 | 3 | 1 | 0.637711 |
7 | 4 | 2 | 0.224989 |
8 | 4 | 2 | 0.0547333 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
Row | v |
---|---|
Float64 | |
1 | 0.452854 |
2 | 0.259274 |
3 | 0.604998 |
4 | 0.663381 |
5 | 0.589787 |
6 | 0.637711 |
7 | 0.224989 |
8 | 0.0547333 |
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.452854 |
2 | 1 | 1 | 0.259274 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.224989 |
2 | 4 | 2 | 0.0547333 |
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.452854
2 │ 1 1 0.259274, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.452854
2 │ 1 1 0.259274, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.452854
2 │ 1 1 0.259274, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.452854
2 │ 1 1 0.259274)
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 | b | 0.687815 |
2 | a | 0.907297 |
3 | d | 0.753421 |
4 | c | 0.746023 |
5 | d | 0.184972 |
6 | d | 0.519753 |
7 | d | 0.819507 |
8 | c | 0.171625 |
9 | b | 0.651596 |
10 | c | 0.578907 |
11 | b | 0.457389 |
12 | a | 0.242115 |
13 | b | 0.929259 |
⋮ | ⋮ | ⋮ |
89 | c | 0.0398338 |
90 | a | 0.504607 |
91 | a | 0.0691471 |
92 | d | 0.0866579 |
93 | b | 0.105838 |
94 | d | 0.761518 |
95 | d | 0.418352 |
96 | c | 0.198222 |
97 | c | 0.354353 |
98 | c | 0.605072 |
99 | c | 0.474177 |
100 | d | 0.277162 |
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 | b | 0.502598 |
2 | a | 0.555364 |
3 | d | 0.509115 |
4 | c | 0.52345 |
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 | b | 0.687815 | 1 | 0.502598 |
2 | a | 0.907297 | 2 | 0.555364 |
3 | d | 0.753421 | 3 | 0.509115 |
4 | c | 0.746023 | 4 | 0.52345 |
5 | d | 0.184972 | 5 | 0.509115 |
6 | d | 0.519753 | 6 | 0.509115 |
7 | d | 0.819507 | 7 | 0.509115 |
8 | c | 0.171625 | 8 | 0.52345 |
9 | b | 0.651596 | 9 | 0.502598 |
10 | c | 0.578907 | 10 | 0.52345 |
11 | b | 0.457389 | 11 | 0.502598 |
12 | a | 0.242115 | 12 | 0.555364 |
13 | b | 0.929259 | 13 | 0.502598 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
89 | c | 0.0398338 | 89 | 0.52345 |
90 | a | 0.504607 | 90 | 0.555364 |
91 | a | 0.0691471 | 91 | 0.555364 |
92 | d | 0.0866579 | 92 | 0.509115 |
93 | b | 0.105838 | 93 | 0.502598 |
94 | d | 0.761518 | 94 | 0.509115 |
95 | d | 0.418352 | 95 | 0.509115 |
96 | c | 0.198222 | 96 | 0.52345 |
97 | c | 0.354353 | 97 | 0.52345 |
98 | c | 0.605072 | 98 | 0.52345 |
99 | c | 0.474177 | 99 | 0.52345 |
100 | d | 0.277162 | 100 | 0.509115 |
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 | b | 1 | 0.502598 |
2 | b | 9 | 0.502598 |
3 | b | 11 | 0.502598 |
4 | b | 13 | 0.502598 |
5 | b | 14 | 0.502598 |
6 | b | 29 | 0.502598 |
7 | b | 31 | 0.502598 |
8 | b | 37 | 0.502598 |
9 | b | 38 | 0.502598 |
10 | b | 39 | 0.502598 |
11 | b | 47 | 0.502598 |
12 | b | 53 | 0.502598 |
13 | b | 58 | 0.502598 |
⋮ | ⋮ | ⋮ | ⋮ |
89 | c | 65 | 0.52345 |
90 | c | 70 | 0.52345 |
91 | c | 74 | 0.52345 |
92 | c | 76 | 0.52345 |
93 | c | 77 | 0.52345 |
94 | c | 85 | 0.52345 |
95 | c | 88 | 0.52345 |
96 | c | 89 | 0.52345 |
97 | c | 96 | 0.52345 |
98 | c | 97 | 0.52345 |
99 | c | 98 | 0.52345 |
100 | c | 99 | 0.52345 |
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 | b | 0.502598 |
2 | a | 0.555364 |
3 | d | 0.509115 |
4 | c | 0.52345 |
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 | b | 0.502598 | 9.54936 | 19 |
2 | a | 0.555364 | 12.7734 | 23 |
3 | d | 0.509115 | 14.2552 | 28 |
4 | c | 0.52345 | 15.7035 | 30 |
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 | 28 |
2 | c | 30 |
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.106617 | 0.291168 | 0.1674 | 0.775344 | 0.943925 | 0.166796 | 0.490957 | 0.53846 | 0.463252 | 0.741993 |
2 | 0.662203 | 0.130301 | 0.992243 | 0.138432 | 0.281077 | 0.438627 | 0.757478 | 0.979952 | 0.508883 | 0.573489 |
3 | 0.136419 | 0.835384 | 0.251823 | 0.318747 | 0.956128 | 0.438975 | 0.439469 | 0.0138696 | 0.191399 | 0.216512 |
4 | 0.575429 | 0.74441 | 0.798861 | 0.60023 | 0.74901 | 0.677474 | 0.259728 | 0.437118 | 0.675392 | 0.50832 |
5 | 0.948811 | 0.166646 | 0.201158 | 0.705186 | 0.681665 | 0.89616 | 0.823825 | 0.695029 | 0.3091 | 0.985866 |
6 | 0.560098 | 0.837752 | 0.0390727 | 0.54336 | 0.187887 | 0.59732 | 0.49907 | 0.515912 | 0.566915 | 0.335263 |
7 | 0.145522 | 0.596191 | 0.0446629 | 0.765127 | 0.852365 | 0.996652 | 0.351631 | 0.865093 | 0.377556 | 0.121889 |
8 | 0.1456 | 0.245567 | 0.441424 | 0.585407 | 0.572443 | 0.90318 | 0.557173 | 0.561593 | 0.818471 | 0.384634 |
9 | 0.90192 | 0.718953 | 0.0471103 | 0.0332092 | 0.152148 | 0.696649 | 0.269912 | 0.379309 | 0.373111 | 0.121797 |
10 | 0.761204 | 0.332511 | 0.845687 | 0.792485 | 0.202757 | 0.131407 | 0.737986 | 0.0398569 | 0.311286 | 0.502011 |
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.494382 | 0.489888 | 0.382944 | 0.525753 | 0.55794 | 0.594324 | 0.518723 | 0.502619 | 0.459537 | 0.449177 |
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.4943824664189296
0.48988820236298974
0.38294400298210396
0.5257527517021974
0.5579404589540388
0.594324121350188
0.5187228558595499
0.5026192006128546
0.45953654364177865
0.4491773322836904
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.4943824664189296
x2: 0.48988820236298974
x3: 0.38294400298210396
x4: 0.5257527517021974
x5: 0.5579404589540388
x6: 0.594324121350188
x7: 0.5187228558595499
x8: 0.5026192006128546
x9: 0.45953654364177865
x10: 0.4491773322836904
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.36617154763225235
5.082108318350985
0.16330072246858027
0.7730008652075995
5.693568306175609
0.6685728829468034
0.2440863067533868
0.5929160989190951
1.2544915085157406
2.2892576622142773
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.10661731577051836
0.6622031498542199
0.1364187656650997
0.5754294556458643
0.9488110585459724
0.5600981459786365
0.14552211973238283
0.14560044116986715
0.9019200455048797
0.7612041663218551
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.106617 | 0.291168 | 0.1674 | 0.775344 | 0.943925 | 0.166796 | 0.490957 | 0.53846 | 0.463252 | 0.741993 |
2 | 0.662203 | 0.130301 | 0.992243 | 0.138432 | 0.281077 | 0.438627 | 0.757478 | 0.979952 | 0.508883 | 0.573489 |
3 | 0.136419 | 0.835384 | 0.251823 | 0.318747 | 0.956128 | 0.438975 | 0.439469 | 0.0138696 | 0.191399 | 0.216512 |
4 | 0.575429 | 0.74441 | 0.798861 | 0.60023 | 0.74901 | 0.677474 | 0.259728 | 0.437118 | 0.675392 | 0.50832 |
5 | 0.948811 | 0.166646 | 0.201158 | 0.705186 | 0.681665 | 0.89616 | 0.823825 | 0.695029 | 0.3091 | 0.985866 |
6 | 0.560098 | 0.837752 | 0.0390727 | 0.54336 | 0.187887 | 0.59732 | 0.49907 | 0.515912 | 0.566915 | 0.335263 |
7 | 0.145522 | 0.596191 | 0.0446629 | 0.765127 | 0.852365 | 0.996652 | 0.351631 | 0.865093 | 0.377556 | 0.121889 |
8 | 0.1456 | 0.245567 | 0.441424 | 0.585407 | 0.572443 | 0.90318 | 0.557173 | 0.561593 | 0.818471 | 0.384634 |
9 | 0.90192 | 0.718953 | 0.0471103 | 0.0332092 | 0.152148 | 0.696649 | 0.269912 | 0.379309 | 0.373111 | 0.121797 |
10 | 0.761204 | 0.332511 | 0.845687 | 0.792485 | 0.202757 | 0.131407 | 0.737986 | 0.0398569 | 0.311286 | 0.502011 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.10661731577051836
0.6622031498542199
0.1364187656650997
0.5754294556458643
0.9488110585459724
0.5600981459786365
0.14552211973238283
0.14560044116986715
0.9019200455048797
0.7612041663218551
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.