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.173134 |
2 | 2 | 2 | 0.0333481 |
3 | 3 | 1 | 0.180963 |
4 | 4 | 2 | 0.113274 |
5 | 1 | 1 | 0.836035 |
6 | 2 | 2 | 0.0155187 |
7 | 3 | 1 | 0.340925 |
8 | 4 | 2 | 0.468375 |
groupby(x, :id)
GroupedDataFrame with 4 groups based on key: id
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.173134 |
2 | 1 | 1 | 0.836035 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.113274 |
2 | 4 | 2 | 0.468375 |
groupby(x, [])
GroupedDataFrame with 1 group based on key:
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.173134 |
2 | 2 | 2 | 0.0333481 |
3 | 3 | 1 | 0.180963 |
4 | 4 | 2 | 0.113274 |
5 | 1 | 1 | 0.836035 |
6 | 2 | 2 | 0.0155187 |
7 | 3 | 1 | 0.340925 |
8 | 4 | 2 | 0.468375 |
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.173134 |
2 | 1 | 1 | 0.836035 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.113274 |
2 | 4 | 2 | 0.468375 |
get the parent DataFrame
parent(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.173134 |
2 | 2 | 2 | 0.0333481 |
3 | 3 | 1 | 0.180963 |
4 | 4 | 2 | 0.113274 |
5 | 1 | 1 | 0.836035 |
6 | 2 | 2 | 0.0155187 |
7 | 3 | 1 | 0.340925 |
8 | 4 | 2 | 0.468375 |
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.173134 |
2 | 1 | 1 | 0.836035 |
3 | 2 | 2 | 0.0333481 |
4 | 2 | 2 | 0.0155187 |
5 | 3 | 1 | 0.180963 |
6 | 3 | 1 | 0.340925 |
7 | 4 | 2 | 0.113274 |
8 | 4 | 2 | 0.468375 |
the same as above
DataFrame(gx2)
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 1 | 1 | 0.173134 |
2 | 1 | 1 | 0.836035 |
3 | 2 | 2 | 0.0333481 |
4 | 2 | 2 | 0.0155187 |
5 | 3 | 1 | 0.180963 |
6 | 3 | 1 | 0.340925 |
7 | 4 | 2 | 0.113274 |
8 | 4 | 2 | 0.468375 |
drop grouping columns when creating a data frame
DataFrame(gx2, keepkeys=false)
Row | v |
---|---|
Float64 | |
1 | 0.173134 |
2 | 0.836035 |
3 | 0.0333481 |
4 | 0.0155187 |
5 | 0.180963 |
6 | 0.340925 |
7 | 0.113274 |
8 | 0.468375 |
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.173134 |
2 | 1 | 1 | 0.836035 |
⋮
Row | id | id2 | v |
---|---|---|---|
Int64 | Int64 | Float64 | |
1 | 4 | 2 | 0.113274 |
2 | 4 | 2 | 0.468375 |
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.173134
2 │ 1 1 0.836035, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.173134
2 │ 1 1 0.836035, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.173134
2 │ 1 1 0.836035, 2×3 SubDataFrame
Row │ id id2 v
│ Int64 Int64 Float64
─────┼────────────────────────
1 │ 1 1 0.173134
2 │ 1 1 0.836035)
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 | d | 0.59953 |
2 | d | 0.963186 |
3 | b | 0.912677 |
4 | c | 0.46845 |
5 | b | 0.640382 |
6 | b | 0.340461 |
7 | c | 0.716181 |
8 | b | 0.748398 |
9 | c | 0.0640978 |
10 | b | 0.706182 |
11 | b | 0.161233 |
12 | c | 0.261802 |
13 | c | 0.15591 |
⋮ | ⋮ | ⋮ |
89 | b | 0.467717 |
90 | a | 0.128613 |
91 | d | 0.401218 |
92 | a | 0.0066471 |
93 | a | 0.609879 |
94 | b | 0.200713 |
95 | c | 0.666582 |
96 | a | 0.770652 |
97 | a | 0.297559 |
98 | c | 0.797879 |
99 | b | 0.350133 |
100 | b | 0.229405 |
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 | d | 0.479639 |
2 | b | 0.502398 |
3 | c | 0.548179 |
4 | a | 0.426175 |
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 | d | 0.59953 | 1 | 0.479639 |
2 | d | 0.963186 | 2 | 0.479639 |
3 | b | 0.912677 | 3 | 0.502398 |
4 | c | 0.46845 | 4 | 0.548179 |
5 | b | 0.640382 | 5 | 0.502398 |
6 | b | 0.340461 | 6 | 0.502398 |
7 | c | 0.716181 | 7 | 0.548179 |
8 | b | 0.748398 | 8 | 0.502398 |
9 | c | 0.0640978 | 9 | 0.548179 |
10 | b | 0.706182 | 10 | 0.502398 |
11 | b | 0.161233 | 11 | 0.502398 |
12 | c | 0.261802 | 12 | 0.548179 |
13 | c | 0.15591 | 13 | 0.548179 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
89 | b | 0.467717 | 89 | 0.502398 |
90 | a | 0.128613 | 90 | 0.426175 |
91 | d | 0.401218 | 91 | 0.479639 |
92 | a | 0.0066471 | 92 | 0.426175 |
93 | a | 0.609879 | 93 | 0.426175 |
94 | b | 0.200713 | 94 | 0.502398 |
95 | c | 0.666582 | 95 | 0.548179 |
96 | a | 0.770652 | 96 | 0.426175 |
97 | a | 0.297559 | 97 | 0.426175 |
98 | c | 0.797879 | 98 | 0.548179 |
99 | b | 0.350133 | 99 | 0.502398 |
100 | b | 0.229405 | 100 | 0.502398 |
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 | d | 1 | 0.479639 |
2 | d | 2 | 0.479639 |
3 | d | 15 | 0.479639 |
4 | d | 20 | 0.479639 |
5 | d | 30 | 0.479639 |
6 | d | 31 | 0.479639 |
7 | d | 32 | 0.479639 |
8 | d | 35 | 0.479639 |
9 | d | 43 | 0.479639 |
10 | d | 48 | 0.479639 |
11 | d | 49 | 0.479639 |
12 | d | 56 | 0.479639 |
13 | d | 58 | 0.479639 |
⋮ | ⋮ | ⋮ | ⋮ |
89 | a | 62 | 0.426175 |
90 | a | 65 | 0.426175 |
91 | a | 67 | 0.426175 |
92 | a | 71 | 0.426175 |
93 | a | 72 | 0.426175 |
94 | a | 75 | 0.426175 |
95 | a | 79 | 0.426175 |
96 | a | 90 | 0.426175 |
97 | a | 92 | 0.426175 |
98 | a | 93 | 0.426175 |
99 | a | 96 | 0.426175 |
100 | a | 97 | 0.426175 |
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 | d | 0.479639 |
2 | b | 0.502398 |
3 | c | 0.548179 |
4 | a | 0.426175 |
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 | d | 0.479639 | 11.0317 | 23 |
2 | b | 0.502398 | 16.5791 | 33 |
3 | c | 0.548179 | 13.1563 | 24 |
4 | a | 0.426175 | 8.5235 | 20 |
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 | b | 33 |
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.406316 | 0.983192 | 0.384771 | 0.92523 | 0.939731 | 0.955698 | 0.366889 | 0.623042 | 0.19006 | 0.846325 |
2 | 0.461318 | 0.630919 | 0.137374 | 0.831208 | 0.250198 | 0.264345 | 0.518362 | 0.262207 | 0.122696 | 0.307633 |
3 | 0.311336 | 0.261178 | 0.991151 | 0.972152 | 0.509253 | 0.66056 | 0.907303 | 0.51472 | 0.412227 | 0.256053 |
4 | 0.419724 | 0.903528 | 0.297772 | 0.515809 | 0.0105733 | 0.000237585 | 0.946771 | 0.463093 | 0.976676 | 0.978568 |
5 | 0.643806 | 0.152396 | 0.636482 | 0.650665 | 0.40173 | 0.515477 | 0.301423 | 0.316934 | 0.0873756 | 0.139309 |
6 | 0.605538 | 0.253532 | 0.253467 | 0.35848 | 0.214893 | 0.268126 | 0.299708 | 0.83004 | 0.69916 | 0.711818 |
7 | 0.503239 | 0.500504 | 0.914121 | 0.697106 | 0.801714 | 0.876675 | 0.190561 | 0.45116 | 0.374046 | 0.0171316 |
8 | 0.699878 | 0.246049 | 0.912662 | 0.219399 | 0.862877 | 0.539972 | 0.554607 | 0.821112 | 0.253741 | 0.277274 |
9 | 0.60524 | 0.828592 | 0.623395 | 0.740417 | 0.296086 | 0.0719441 | 0.181372 | 0.328845 | 0.697748 | 0.983598 |
10 | 0.404152 | 0.118663 | 0.918418 | 0.659753 | 0.242187 | 0.850887 | 0.737558 | 0.0118075 | 0.136959 | 0.544532 |
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.506055 | 0.487855 | 0.606961 | 0.657022 | 0.452924 | 0.500392 | 0.500455 | 0.462296 | 0.395069 | 0.506224 |
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.5060547920796004
0.48785535814481973
0.6069613476270621
0.6570220327155232
0.45292425664895275
0.5003921008238743
0.5004553926468702
0.4622961689025299
0.39506884836796574
0.5062243237787427
an iteration returns a Pair with column name and values
foreach(c -> println(c[1], ": ", mean(c[2])), pairs(eachcol(x)))
x1: 0.5060547920796004
x2: 0.48785535814481973
x3: 0.6069613476270621
x4: 0.6570220327155232
x5: 0.45292425664895275
x6: 0.5003921008238743
x7: 0.5004553926468702
x8: 0.4622961689025299
x9: 0.39506884836796574
x10: 0.5062243237787427
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.4132624389343559
0.7311832529054909
1.1920438782095928
0.46453955353950066
4.224551393630487
2.388414434655677
1.0054654848736022
2.8444695221189074
0.730443081489957
3.4058671583882494
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.4063163827820543
0.4613176613354725
0.31133551371983115
0.4197243987932313
0.6438063265650948
0.6055384860738079
0.5032393113313084
0.6998783593017472
0.6052395617941637
0.40415191909929205
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.406316 | 0.983192 | 0.384771 | 0.92523 | 0.939731 | 0.955698 | 0.366889 | 0.623042 | 0.19006 | 0.846325 |
2 | 0.461318 | 0.630919 | 0.137374 | 0.831208 | 0.250198 | 0.264345 | 0.518362 | 0.262207 | 0.122696 | 0.307633 |
3 | 0.311336 | 0.261178 | 0.991151 | 0.972152 | 0.509253 | 0.66056 | 0.907303 | 0.51472 | 0.412227 | 0.256053 |
4 | 0.419724 | 0.903528 | 0.297772 | 0.515809 | 0.0105733 | 0.000237585 | 0.946771 | 0.463093 | 0.976676 | 0.978568 |
5 | 0.643806 | 0.152396 | 0.636482 | 0.650665 | 0.40173 | 0.515477 | 0.301423 | 0.316934 | 0.0873756 | 0.139309 |
6 | 0.605538 | 0.253532 | 0.253467 | 0.35848 | 0.214893 | 0.268126 | 0.299708 | 0.83004 | 0.69916 | 0.711818 |
7 | 0.503239 | 0.500504 | 0.914121 | 0.697106 | 0.801714 | 0.876675 | 0.190561 | 0.45116 | 0.374046 | 0.0171316 |
8 | 0.699878 | 0.246049 | 0.912662 | 0.219399 | 0.862877 | 0.539972 | 0.554607 | 0.821112 | 0.253741 | 0.277274 |
9 | 0.60524 | 0.828592 | 0.623395 | 0.740417 | 0.296086 | 0.0719441 | 0.181372 | 0.328845 | 0.697748 | 0.983598 |
10 | 0.404152 | 0.118663 | 0.918418 | 0.659753 | 0.242187 | 0.850887 | 0.737558 | 0.0118075 | 0.136959 | 0.544532 |
you can access columns of a parent data frame directly
ec.x1
10-element Vector{Float64}:
0.4063163827820543
0.4613176613354725
0.31133551371983115
0.4197243987932313
0.6438063265650948
0.6055384860738079
0.5032393113313084
0.6998783593017472
0.6052395617941637
0.40415191909929205
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.