Skip to content
Julia

DataFrame Operations (DataFrames.jl)

Filter, transform, group, and join tabular data with DataFrames.jl.

By EZ4Code Team
dataframedatatables

Code

using DataFrames, Statistics

df = DataFrame(
    name = ["Alice", "Bob", "Carol", "Dave"],
    dept = ["Eng", "Eng", "Sales", "Sales"],
    salary = [90, 85, 70, 75],
    years = [5, 3, 7, 2],
)

# Filter rows
eng = filter(:dept => ==("Eng"), df)
high = filter([:salary, :years] => (s, y) -> s > 80 && y > 2, df)

# Add / transform columns (with! is in-place)
transform!(df, :salary => ByRow(x -> x * 1.1) => :raise)
transform!(df, [:salary, :years] => ByRow((s, y) -> s / y) => :per_year)

# Group + summarize
gdf = groupby(df, :dept)
combine(gdf, :salary => mean => :avg_salary,
           :salary => sum  => :total,
           nrow            => :headcount)

# Sort
sort!(df, :salary, rev=true)

# Join
bonuses = DataFrame(name=["Alice","Bob","Eve"], bonus=[10, 5, 8])
joined = innerjoin(df, bonuses, on = :name)

Explanation

DataFrames.jl mirrors R's dplyr / Python's pandas but uses Julia's types for speed. The column => function => target syntax of transform/combine is the central idiom: it specifies the input column(s), the transformation (often wrapped in ByRow to apply row-wise), and the output column name. groupby + combine is the split-apply-combine pattern for grouped summaries.

More Julia Snippets