programming
Dataframe

DataFrames.jl 😋

In short and sweet, DataFrames is pandas for julia

  • Query.jl
  • CSV.jl
    using DataFrames

To create random data

df = DataFrame(A=1:5,B=["M","J","O","G","I"])
 
# to access "A"
df.A
df."A"
 
# to get names 
name(df)

constructing column by column

df = DataFrame()
df.A = 1:8
df[:,:B] = [1,2,3,4,5,6,7,8]  # it will add A,B to df

To write a csv file

using CSV
CSV.write("filename.csv",df)

Tail & Head

first(df,5)
last(df,5)

Selecting and Transforming Column

select(df,Not(:A)) # drop column A in a new data frame
 
# columns = ["x1","x2","x3","y1","y2"]
select(df, r"x") # select columns containing 'x' character
 
select(df, :x1 => :a1, :x2 => :a2) # rename columns

Query

df = DataFrame(name=["John", "Sally", "Roger"],
                      age=[54.0, 34.0, 79.0],
                      children=[0, 2, 4])
 
q1 = @from i in df begin
            @where i.age > 40
            @select {number_of_children=i.children, i.name}
            @collect DataFrame
       end
 
 
q2 = @from i in df begin
                   @where i.age > 40
                   @select {number_of_children=i.children, i.name}
              end; # suppress printing the iterator type    
 
y = [i.name for i in q2 if i.number_of_children > 0] # loop like python

Julia functions

iseven()
 

Random

df[1:3,[:B,:A]]
df[1:3,[:C]]
df[[3:1],[:C]]