9  Data Frame

9.1 Create DF

using DataFrames

9.1.1 Using Column Vector

df = DataFrame(
    ID = 1:4, 
    Subject = repeat(["M", "F"], outer=2),
    Is_OK = true
    )

4 rows × 3 columns

IDSubjectIs_OK
Int64StringBool
11M1
22F1
33M1
44F1
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_agefirst_name
Int64String
115Rohit
220Rahul
325Akshat

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:

df[:, :ID]
4-element Vector{Int64}:
 1
 2
 3
 4

This will modify df in-place:

df[!, :ID][1] = 5
5
df

4 rows × 3 columns

IDSubjectIs_OK
Int64StringBool
15M1
22F1
33M1
44F1