Julia DataFrames
last modified November 18, 2026
The DataFrames package in Julia is a robust tool for handling
structured data, akin to pandas in Python or data frames in R. This tutorial
dives into both basic and advanced data manipulation techniques with practical,
real-world examples. The examples were run with Julia 1.13 and DataFrames 1.8.3.
With DataFrames, you can clean messy datasets, transform data for
analysis, and perform complex operations like joins and aggregations. It's a
cornerstone for data science in Julia.
Install DataFrames
Let's start by installing the DataFrames package in Julia.
using Pkg
Pkg.add("DataFrames")
The using Pkg line loads Julia's package manager. Then,
Pkg.add("DataFrames") fetches and installs the DataFrames package
from Julia's package registry. Run this in the Julia REPL or a script; the
output depends on your Julia version and which packages are already installed,
so it is not shown here. The CSV examples below need Pkg.add("CSV")
as well.
Create a DataFrame
Here's how to create a DataFrame representing employee records.
using DataFrames
df = DataFrame(
EmployeeID = [101, 102, 103],
Name = ["Alice Smith", "Bob Jones", "Clara Lee"],
Department = ["Sales", "IT", "HR"],
Salary = [55000.0, 72000.0, 48000.0]
)
println(df)
This creates a DataFrame with four columns: EmployeeID,
Name, Department, and Salary. Each column
is an array of values. The println(df) line displays the table in
the console, showing a neatly formatted output with the column types beneath
their names.
This example mimics a small company dataset, making it relatable for business data analysis tasks.
$ julia create.jl
3×4 DataFrame
Row │ EmployeeID Name Department Salary
│ Int64 String String Float64
─────┼──────────────────────────────────────────────
1 │ 101 Alice Smith Sales 55000.0
2 │ 102 Bob Jones IT 72000.0
3 │ 103 Clara Lee HR 48000.0
Load Data from CSV
Loading real-world data often involves reading CSV files. Here's an example that first writes a small CSV file and then reads it back, so it can be run as it stands.
using CSV, DataFrames
CSV.write("sales_data.csv", DataFrame(
OrderID = [1, 2, 3],
Product = ["Laptop", "Mouse", "Monitor"],
Quantity = [5, 20, 3],
Price = [1200.0, 25.0, 300.0]
))
df = CSV.read("sales_data.csv", DataFrame)
df[!, :Product] = String.(df.Product)
println(first(df, 2))
First, include CSV and DataFrames. The
CSV.read function loads "sales_data.csv" into a DataFrame. The
file contains the columns OrderID, Product,
Quantity, and Price. Recent versions of CSV return a
dedicated string type for text columns, so we convert Product to
the ordinary String type for a stable result.
println(first(df, 2)) shows the first 2 rows, useful for quickly
inspecting large datasets. This is a common task in data analysis workflows.
$ julia load_csv.jl
2×4 DataFrame
Row │ OrderID Product Quantity Price
│ Int64 String Int64 Float64
─────┼─────────────────────────────────────
1 │ 1 Laptop 5 1200.0
2 │ 2 Mouse 20 25.0
Filter Rows
Filtering is key for extracting relevant data. Here's an example with sales.
using DataFrames
df = DataFrame(
OrderID = [1, 2, 3],
Product = ["Laptop", "Mouse", "Monitor"],
Quantity = [5, 20, 3],
Price = [1200.0, 25.0, 300.0]
)
high_value = filter(row -> row.Price * row.Quantity > 1000, df)
println(high_value)
This creates a sample sales DataFrame. The filter function selects
rows where the total value (Price * Quantity) exceeds 1000. The
lambda function row -> row.Price * row.Quantity > 1000
evaluates each row and keeps those for which it returns true.
Here only the "Laptop" order qualifies: 1200.0 × 5 is 6000. The "Mouse" order totals 500 and the "Monitor" order totals 900, so both are excluded. This is how filtering can identify high-value transactions in a business context.
$ julia filter.jl
1×4 DataFrame
Row │ OrderID Product Quantity Price
│ Int64 String Int64 Float64
─────┼─────────────────────────────────────
1 │ 1 Laptop 5 1200.0
Sort DataFrame
Sorting helps organize data. Here's how to sort employees by salary.
using DataFrames
df = DataFrame(
Name = ["Alice", "Bob", "Clara"],
Salary = [55000, 72000, 48000]
)
sorted_df = sort(df, :Salary, rev=true)
println(sorted_df)
The sort function orders the DataFrame by Salary. The
rev=true argument sorts in descending order, so Bob (72000) appears
first, followed by Alice (55000) and Clara (48000).
This is useful for ranking employees by compensation or prioritizing high-cost items in a budget analysis.
$ julia sort.jl
3×2 DataFrame
Row │ Name Salary
│ String Int64
─────┼────────────────
1 │ Bob 72000
2 │ Alice 55000
3 │ Clara 48000
Add a New Column
Adding columns enhances data. Here, we calculate annual bonuses.
using DataFrames
df = DataFrame(
Name = ["Alice", "Bob"],
Salary = [55000.0, 72000.0]
)
df[!, :Bonus] = df.Salary .* 0.1
println(df)
The df[!, :Bonus] syntax adds a Bonus column. We
calculate it as 10% of Salary using the element-wise multiplication
operator .*. Alice gets 5500.0, Bob gets 7200.0.
The ! means the operation modifies df in place,
which is efficient for large datasets.
$ julia add_column.jl
2×3 DataFrame
Row │ Name Salary Bonus
│ String Float64 Float64
─────┼──────────────────────────
1 │ Alice 55000.0 5500.0
2 │ Bob 72000.0 7200.0
Group and Aggregate
Grouping summarizes data. Here's an example with sales by product category.
using DataFrames, Statistics
df = DataFrame(
Category = ["Electronics", "Electronics", "Office"],
Sales = [1200.0, 300.0, 50.0]
)
grouped = combine(groupby(df, :Category), :Sales => mean => :AvgSales)
println(grouped)
The groupby function groups rows by Category.
combine then computes the mean of Sales per group,
renaming it AvgSales. Electronics averages 750.0, Office 50.0.
This is practical for summarizing sales performance across product lines in a retail dataset.
$ julia group.jl
2×2 DataFrame
Row │ Category AvgSales
│ String Float64
─────┼───────────────────────
1 │ Electronics 750.0
2 │ Office 50.0
Join DataFrames
Joining combines datasets. Here, we merge employee and department data.
using DataFrames
employees = DataFrame(
ID = [101, 102],
Name = ["Alice", "Bob"]
)
depts = DataFrame(
ID = [101, 102],
Dept = ["Sales", "IT"]
)
joined = innerjoin(employees, depts, on=:ID)
println(joined)
We create two DataFrames: employees with names and IDs, and
depts with department info. innerjoin merges them on
ID, keeping only matching rows.
The result pairs Alice with Sales and Bob with IT, simulating a real HR system lookup.
$ julia join.jl
2×3 DataFrame
Row │ ID Name Dept
│ Int64 String String
─────┼───────────────────────
1 │ 101 Alice Sales
2 │ 102 Bob IT
Handle Missing Data
Real data often has gaps. Here's how to handle missing values.
using DataFrames
df = DataFrame(
Name = ["Alice", "Bob", "Clara"],
Score = [85, missing, 90]
)
cleaned = dropmissing(df)
println(cleaned)
The DataFrame has a missing value in Score.
dropmissing removes rows with any missing data, so Bob's row is
excluded, leaving Alice and Clara.
This is critical for preparing data for statistical models that can't handle missing values.
$ julia missing.jl
2×2 DataFrame
Row │ Name Score
│ String Int64
─────┼───────────────
1 │ Alice 85
2 │ Clara 90
Export Data to CSV
Saving results is common. Here's how to export a DataFrame to CSV.
using CSV, DataFrames
df = DataFrame(
Product = ["Laptop", "Mouse"],
Price = [1200.0, 25.0]
)
CSV.write("output.csv", df)
print(read("output.csv", String))
CSV.write saves the DataFrame to "output.csv". This file can be
opened in Excel or shared with colleagues, making it a practical step in data
workflows. We read the file back with read and print it so the
result of the write is visible here; the first line holds the column names.
$ julia export.jl Product,Price Laptop,1200.0 Mouse,25.0
Best Practices for DataFrames
A few habits make DataFrame code easier to read and more reliable.
- Use Descriptive Column Names: Prefer
TotalSalesoverTSfor clarity. - Handle Missing Data: Use
dropmissingorreplaceto clean data. - Optimize Performance: Use
@timeto benchmark operations on large datasets. - Document Your Code: Comment steps like "Group by region to calculate average revenue".
Source
Julia DataFrames documentation
This tutorial covered practical uses of DataFrames, from creating
and filtering data to grouping and exporting, all with realistic examples. The
collections that hold the individual values are described in
Julia arrays and
Julia dictionaries; the functions used in
transformations are introduced in Julia functions.
Author
List all Julia tutorials.