using DataFrames9 Data Frame
9.1 Create DF
9.1.1 Using Column Vector
df = DataFrame(
ID = 1:4,
Subject = repeat(["M", "F"], outer=2),
Is_OK = true
)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 = Dict(:first_name => ["Rohit", "Rahul", "Akshat"],
:customer_age => [15, 20, 25]
)
df2 = DataFrame(dict)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)) |> DictDict{Symbol, AbstractVector} with 2 entries:
:customer_age => [15, 20, 25]
:first_name => ["Rohit", "Rahul", "Akshat"]
pairs(df2.first_name) |> DictDict{Int64, String} with 3 entries:
2 => "Rahul"
3 => "Akshat"
1 => "Rohit"
Pair.(df2.first_name, df2.customer_age) |> DictDict{String, Int64} with 3 entries:
"Akshat" => 25
"Rahul" => 20
"Rohit" => 15
9.2 Subsetting
9.2.1 By Column
df.ID4-element Vector{Int64}:
1
2
3
4
Return a copy of the column by:
df[:, :ID]4-element Vector{Int64}:
1
2
3
4
This will modify df in-place:
df[!, :ID][1] = 55
df4 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 |