How to View the First and Last Parts of a DataFrame in Julia
Overview
In Julia, DataFrames do not have head or tail functions. Although it’s a bit annoying because it requires an extra step compared to other languages, you can use the first and last functions. Why does DataFrames.jl stubbornly ignore industry conventions? This can roughly be summarized as follows1:
:
- There’s already a
Base.tailfunction inBase, and they want to avoid conflicts. firstandlastwere already implemented for the same purpose beforeheadandtail.- It’s preferable to keep function names as concise as possible, and
headortailare too common and short.
Code
using DataFrames
df = DataFrame(reshape(1:100, :, 5), :auto)
first
julia> first(df, 3)
3×5 DataFrame
Row │ x1 x2 x3 x4 x5
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
1 │ 1 21 41 61 81
2 │ 2 22 42 62 82
3 │ 3 23 43 63 83
last
julia> last(df, 10)
10×5 DataFrame
Row │ x1 x2 x3 x4 x5
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
1 │ 11 31 51 71 91
2 │ 12 32 52 72 92
3 │ 13 33 53 73 93
4 │ 14 34 54 74 94
5 │ 15 35 55 75 95
6 │ 16 36 56 76 96
7 │ 17 37 57 77 97
8 │ 18 38 58 78 98
9 │ 19 39 59 79 99
10 │ 20 40 60 80 100
If the second argument is omitted, it defaults to showing just one row.
julia> last(df)
DataFrameRow
Row │ x1 x2 x3 x4 x5
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
20 │ 20 40 60 80 100
Environment
- OS: Windows
- Julia: v1.11.1
- DataFrames v1.7.0
