using DataFrames
9 Data Frame
9.1 Create DF
9.1.1 Using Column Vector
= DataFrame(
df = 1:4,
ID = repeat(["M", "F"], outer=2),
Subject = true
Is_OK )
4 rows × 3 columns
ID | Subject | Is_OK | |
---|---|---|---|
Int64 | String | Bool | |
1 | 1 | M | 1 |
2 | 2 | F | 1 |
3 | 3 | M | 1 |
4 | 4 | F | 1 |
typeof(df)
DataFrame
names(df)
3-element Vector{String}:
"ID"
"Subject"
"Is_OK"
9.1.2 Create from Dict
= Dict(:first_name => ["Rohit", "Rahul", "Akshat"],
dict :customer_age => [15, 20, 25]
)= DataFrame(dict) df2
3 rows × 2 columns
customer_age | first_name | |
---|---|---|
Int64 | String | |
1 | 15 | Rohit |
2 | 20 | Rahul |
3 | 25 | Akshat |
Convert df2
back to Dict
(Ref)
pairs(eachcol(df2)) |> Dict
Dict{Symbol, AbstractVector} with 2 entries:
:customer_age => [15, 20, 25]
:first_name => ["Rohit", "Rahul", "Akshat"]
pairs(df2.first_name) |> Dict
Dict{Int64, String} with 3 entries:
2 => "Rahul"
3 => "Akshat"
1 => "Rohit"
Pair.(df2.first_name, df2.customer_age) |> Dict
Dict{String, Int64} with 3 entries:
"Akshat" => 25
"Rahul" => 20
"Rohit" => 15
9.2 Subsetting
9.2.1 By Column
df.ID
4-element Vector{Int64}:
1
2
3
4
Return a copy of the column by:
:, :ID] df[
4-element Vector{Int64}:
1
2
3
4
This will modify df
in-place:
:ID][1] = 5 df[!,
5
df
4 rows × 3 columns
ID | Subject | Is_OK | |
---|---|---|---|
Int64 | String | Bool | |
1 | 5 | M | 1 |
2 | 2 | F | 1 |
3 | 3 | M | 1 |
4 | 4 | F | 1 |