Functions
Base.filter
Base.filter!
Base.join
Base.show
Base.sort
Base.sort!
Base.unique!
DataFrames.aggregate
DataFrames.allowmissing!
DataFrames.by
DataFrames.colwise
DataFrames.combine
DataFrames.completecases
DataFrames.disallowmissing!
DataFrames.dropmissing
DataFrames.dropmissing!
DataFrames.eachrow
DataFrames.eltypes
DataFrames.groupby
DataFrames.head
DataFrames.melt
DataFrames.meltdf
DataFrames.names!
DataFrames.nonunique
DataFrames.permutecols!
DataFrames.rename
DataFrames.rename!
DataFrames.stack
DataFrames.stackdf
DataFrames.tail
DataFrames.unstack
StatsBase.describe
Grouping, Joining, and Split-Apply-Combine
DataFrames.aggregate
— Function.Split-apply-combine that applies a set of functions over columns of an AbstractDataFrame or GroupedDataFrame
aggregate(d::AbstractDataFrame, cols, fs)
aggregate(gd::GroupedDataFrame, fs)
Arguments
d
: an AbstractDataFramegd
: a GroupedDataFramecols
: a column indicator (Symbol, Int, Vector{Symbol}, etc.)fs
: a function or vector of functions to be applied to vectors within groups; expects each argument to be a column vector
Each fs
should return a value or vector. All returns must be the same length.
Returns
::DataFrame
Examples
df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
aggregate(df, :a, sum)
aggregate(df, :a, [sum, x->mean(skipmissing(x))])
aggregate(groupby(df, :a), [sum, x->mean(skipmissing(x))])
DataFrames.by
— Function.Split-apply-combine in one step; apply f
to each grouping in d
based on columns col
by(d::AbstractDataFrame, cols, f::Function; sort::Bool = false)
by(f::Function, d::AbstractDataFrame, cols; sort::Bool = false)
Arguments
d
: an AbstractDataFramecols
: a column indicator (Symbol, Int, Vector{Symbol}, etc.)f
: a function to be applied to groups; expects each argument to be an AbstractDataFramesort
: sort row groups (no sorting by default)
f
can return a value, a vector, or a DataFrame. For a value or vector, these are merged into a column along with the cols
keys. For a DataFrame, cols
are combined along columns with the resulting DataFrame. Returning a DataFrame is the clearest because it allows column labeling.
A method is defined with f
as the first argument, so do-block notation can be used.
by(d, cols, f)
is equivalent to combine(map(f, groupby(d, cols)))
.
Returns
::DataFrame
Examples
df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
by(df, :a, d -> sum(d[:c]))
by(df, :a, d -> 2 * skipmissing(d[:c]))
by(df, :a, d -> DataFrame(c_sum = sum(d[:c]), c_mean = mean(skipmissing(d[:c]))))
by(df, :a, d -> DataFrame(c = d[:c], c_mean = mean(skipmissing(d[:c]))))
by(df, [:a, :b]) do d
DataFrame(m = mean(skipmissing(d[:c])), v = var(skipmissing(d[:c])))
end
DataFrames.colwise
— Function.Apply a function to each column in an AbstractDataFrame or GroupedDataFrame
colwise(f::Function, d)
colwise(d)
Arguments
f
: a function or vector of functionsd
: an AbstractDataFrame of GroupedDataFrame
If d
is not provided, a curried version of groupby is given.
Returns
- various, depending on the call
Examples
df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
colwise(sum, df)
colwise([sum, length], df)
colwise((minimum, maximum), df)
colwise(sum, groupby(df, :a))
DataFrames.groupby
— Function.A view of an AbstractDataFrame split into row groups
groupby(d::AbstractDataFrame, cols; sort = false, skipmissing = false)
groupby(cols; sort = false, skipmissing = false)
Arguments
d
: an AbstractDataFrame to split (optional, see Returns)cols
: data table columns to group bysort
: whether to sort rows according to the values of the grouping columnscols
skipmissing
: whether to skip rows withmissing
values in one of the grouping columnscols
Returns
::GroupedDataFrame
: a grouped view intod
::Function
: a functionx -> groupby(x, cols)
(ifd
is not specified)
Details
An iterator over a GroupedDataFrame
returns a SubDataFrame
view for each grouping into d
. A GroupedDataFrame
also supports indexing by groups and map
.
See the following for additional split-apply-combine operations:
by
: split-apply-combine using functionsaggregate
: split-apply-combine; applies functions in the form of a cross productcombine
: combine (obviously)colwise
: apply a function to each column in an AbstractDataFrame or GroupedDataFrame
Examples
df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
gd = groupby(df, :a)
gd[1]
last(gd)
vcat([g[:b] for g in gd]...)
for g in gd
println(g)
end
map(d -> mean(skipmissing(d[:c])), gd) # returns a GroupApplied object
combine(map(d -> mean(skipmissing(d[:c])), gd))
Base.join
— Function.join(df1, df2; on = Symbol[], kind = :inner, makeunique = false,
indicator = nothing, validate = (false, false))
Join two DataFrame
objects
Arguments
df1
,df2
: the two AbstractDataFrames to be joined
Keyword Arguments
on
: A column, or vector of columns to join df1 and df2 on. If the column(s) that df1 and df2 will be joined on have different names, then the columns should be(left, right)
tuples orleft => right
pairs, or a vector of such tuples or pairs.on
is a required argument for all joins except forkind = :cross
kind
: the type of join, options include::inner
: only include rows with keys that match in bothdf1
anddf2
, the default:outer
: include all rows fromdf1
anddf2
:left
: include all rows fromdf1
:right
: include all rows fromdf2
:semi
: return rows ofdf1
that match with the keys indf2
:anti
: return rows ofdf1
that do not match with the keys indf2
:cross
: a full Cartesian product of the key combinations; every row ofdf1
is matched with every row ofdf2
makeunique
: iffalse
(the default), an error will be raised if duplicate names are found in columns not joined on; iftrue
, duplicate names will be suffixed with_i
(i
starting at 1 for the first duplicate).indicator
: Default:nothing
. If aSymbol
, adds categorical indicator column namedSymbol
for whether a row appeared in onlydf1
("left_only"
), onlydf2
("right_only"
) or in both ("both"
). IfSymbol
is already in use, the column name will be modified ifmakeunique=true
.validate
: whether to check that columns passed as theon
argument define unique keys in each input data frame (according toisequal
). Can be a tuple or a pair, with the first element indicating whether to run check fordf1
and the second element fordf2
. By default no check is performed.
For the three join operations that may introduce missing values (:outer
, :left
, and :right
), all columns of the returned data table will support missing values.
When merging on
categorical columns that differ in the ordering of their levels, the ordering of the left DataFrame
takes precedence over the ordering of the right DataFrame
Result
::DataFrame
: the joined DataFrame
Examples
name = DataFrame(ID = [1, 2, 3], Name = ["John Doe", "Jane Doe", "Joe Blogs"])
job = DataFrame(ID = [1, 2, 4], Job = ["Lawyer", "Doctor", "Farmer"])
join(name, job, on = :ID)
join(name, job, on = :ID, kind = :outer)
join(name, job, on = :ID, kind = :left)
join(name, job, on = :ID, kind = :right)
join(name, job, on = :ID, kind = :semi)
join(name, job, on = :ID, kind = :anti)
join(name, job, kind = :cross)
job2 = DataFrame(identifier = [1, 2, 4], Job = ["Lawyer", "Doctor", "Farmer"])
join(name, job2, on = (:ID, :identifier))
join(name, job2, on = :ID => :identifier)
DataFrames.melt
— Function.Stacks a DataFrame; convert from a wide to long format; see stack
.
DataFrames.stack
— Function.Stacks a DataFrame; convert from a wide to long format
stack(df::AbstractDataFrame, [measure_vars], [id_vars];
variable_name::Symbol=:variable, value_name::Symbol=:value)
melt(df::AbstractDataFrame, [id_vars], [measure_vars];
variable_name::Symbol=:variable, value_name::Symbol=:value)
Arguments
df
: the AbstractDataFrame to be stackedmeasure_vars
: the columns to be stacked (the measurement variables), a normal column indexing type, like a Symbol, Vector{Symbol}, Int, etc.; formelt
, defaults to all variables that are notid_vars
. If neithermeasure_vars
orid_vars
are given,measure_vars
defaults to all floating point columns.id_vars
: the identifier columns that are repeated during stacking, a normal column indexing type; forstack
defaults to all variables that are notmeasure_vars
variable_name
: the name of the new stacked column that shall hold the names of each ofmeasure_vars
value_name
: the name of the new stacked column containing the values from each ofmeasure_vars
Result
::DataFrame
: the long-format DataFrame with column:value
holding the values of the stacked columns (measure_vars
), with column:variable
a Vector of Symbols with themeasure_vars
name, and with columns for each of theid_vars
.
See also stackdf
and meltdf
for stacking methods that return a view into the original DataFrame. See unstack
for converting from long to wide format.
Examples
d1 = DataFrame(a = repeat([1:3;], inner = [4]),
b = repeat([1:4;], inner = [3]),
c = randn(12),
d = randn(12),
e = map(string, 'a':'l'))
d1s = stack(d1, [:c, :d])
d1s2 = stack(d1, [:c, :d], [:a])
d1m = melt(d1, [:a, :b, :e])
d1s_name = melt(d1, [:a, :b, :e], variable_name=:somemeasure)
DataFrames.unstack
— Function.Unstacks a DataFrame; convert from a long to wide format
unstack(df::AbstractDataFrame, rowkeys::Union{Symbol, Integer},
colkey::Union{Symbol, Integer}, value::Union{Symbol, Integer})
unstack(df::AbstractDataFrame, rowkeys::AbstractVector{<:Union{Symbol, Integer}},
colkey::Union{Symbol, Integer}, value::Union{Symbol, Integer})
unstack(df::AbstractDataFrame, colkey::Union{Symbol, Integer},
value::Union{Symbol, Integer})
unstack(df::AbstractDataFrame)
Arguments
df
: the AbstractDataFrame to be unstackedrowkeys
: the column(s) with a unique key for each row, if not given, find a key by grouping on anything not acolkey
orvalue
colkey
: the column holding the column names in wide format, defaults to:variable
value
: the value column, defaults to:value
Result
::DataFrame
: the wide-format DataFrame
If colkey
contains missing
values then they will be skipped and a warning will be printed.
If combination of rowkeys
and colkey
contains duplicate entries then last value
will be retained and a warning will be printed.
Examples
wide = DataFrame(id = 1:12,
a = repeat([1:3;], inner = [4]),
b = repeat([1:4;], inner = [3]),
c = randn(12),
d = randn(12))
long = stack(wide)
wide0 = unstack(long)
wide1 = unstack(long, :variable, :value)
wide2 = unstack(long, :id, :variable, :value)
wide3 = unstack(long, [:id, :a], :variable, :value)
Note that there are some differences between the widened results above.
DataFrames.stackdf
— Function.A stacked view of a DataFrame (long format)
Like stack
and melt
, but a view is returned rather than data copies.
stackdf(df::AbstractDataFrame, [measure_vars], [id_vars];
variable_name::Symbol=:variable, value_name::Symbol=:value)
meltdf(df::AbstractDataFrame, [id_vars], [measure_vars];
variable_name::Symbol=:variable, value_name::Symbol=:value)
Arguments
df
: the wide AbstractDataFramemeasure_vars
: the columns to be stacked (the measurement variables), a normal column indexing type, like a Symbol, Vector{Symbol}, Int, etc.; formelt
, defaults to all variables that are notid_vars
id_vars
: the identifier columns that are repeated during stacking, a normal column indexing type; forstack
defaults to all variables that are notmeasure_vars
Result
::DataFrame
: the long-format DataFrame with column:value
holding the values of the stacked columns (measure_vars
), with column:variable
a Vector of Symbols with themeasure_vars
name, and with columns for each of theid_vars
.
The result is a view because the columns are special AbstractVectors that return indexed views into the original DataFrame.
Examples
d1 = DataFrame(a = repeat([1:3;], inner = [4]),
b = repeat([1:4;], inner = [3]),
c = randn(12),
d = randn(12),
e = map(string, 'a':'l'))
d1s = stackdf(d1, [:c, :d])
d1s2 = stackdf(d1, [:c, :d], [:a])
d1m = meltdf(d1, [:a, :b, :e])
DataFrames.meltdf
— Function.A stacked view of a DataFrame (long format); see stackdf
Basics
DataFrames.allowmissing!
— Function.allowmissing!(df::DataFrame)
Convert all columns of a df
from element type T
to Union{T, Missing}
to support missing values.
allowmissing!(df::DataFrame, col::Union{Integer, Symbol})
Convert a single column of a df
from element type T
to Union{T, Missing}
to support missing values.
allowmissing!(df::DataFrame, cols::AbstractVector{<:Union{Integer, Symbol}})
Convert multiple columns of a df
from element type T
to Union{T, Missing}
to support missing values.
DataFrames.combine
— Function.Combine a GroupApplied object (rudimentary)
combine(ga::GroupApplied)
Arguments
ga
: a GroupApplied
Returns
::DataFrame
Examples
df = DataFrame(a = repeat([1, 2, 3, 4], outer=[2]),
b = repeat([2, 1], outer=[4]),
c = randn(8))
gd = groupby(df, :a)
combine(map(d -> mean(skipmissing(d[:c])), gd))
DataFrames.completecases
— Function.Indexes of complete cases (rows without missing values)
completecases(df::AbstractDataFrame)
Arguments
df
: the AbstractDataFrame
Result
::Vector{Bool}
: indexes of complete cases
See also dropmissing
and dropmissing!
.
Examples
df = DataFrame(i = 1:10,
x = Vector{Union{Missing, Float64}}(rand(10)),
y = Vector{Union{Missing, String}}(rand(["a", "b", "c"], 10)))
df[[1,4,5], :x] = missing
df[[9,10], :y] = missing
completecases(df)
StatsBase.describe
— Function.Report descriptive statistics for a data frame
describe(df::AbstractDataFrame; stats = [:mean, :min, :median, :max, :nmissing, :nunique, :eltype])
Arguments
df
: the AbstractDataFramestats::Union{Symbol,AbstractVector{Symbol}}
: the summary statistics to report. If a vector, allowed fields are:mean
,:std
,:min
,:q25
,:median
,:q75
,:max
,:eltype
,:nunique
,:first
,:last
, and:nmissing
. If set to:all
, all summary statistics are reported.
Result
- A
DataFrame
where each row represents a variable and each column a summary statistic.
Details
For Real
columns, compute the mean, standard deviation, minimum, first quantile, median, third quantile, and maximum. If a column does not derive from Real
, describe
will attempt to calculate all statistics, using nothing
as a fall-back in the case of an error.
When stats
contains :nunique
, describe
will report the number of unique values in a column. If a column's base type derives from Real
, :nunique
will return nothing
s.
Missing values are filtered in the calculation of all statistics, however the column :nmissing
will report the number of missing values of that variable. If the column does not allow missing values, nothing
is returned. Consequently, nmissing = 0
indicates that the column allows missing values, but does not currently contain any.
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
describe(df)
describe(df, stats = :all)
describe(df, stats = [:min, :max])
DataFrames.disallowmissing!
— Function.disallowmissing!(df::DataFrame)
Convert all columns of a df
from element type Union{T, Missing}
to T
to drop support for missing values.
disallowmissing!(df::DataFrame, col::Union{Integer, Symbol})
Convert a single column of a df
from element type Union{T, Missing}
to T
to drop support for missing values.
disallowmissing!(df::DataFrame, cols::AbstractVector{<:Union{Integer, Symbol}})
Convert multiple columns of a df
from element type Union{T, Missing}
to T
to drop support for missing values.
DataFrames.dropmissing
— Function.Remove rows with missing values.
dropmissing(df::AbstractDataFrame)
Arguments
df
: the AbstractDataFrame
Result
::AbstractDataFrame
: the updated copy
See also completecases
and dropmissing!
.
Examples
df = DataFrame(i = 1:10,
x = Vector{Union{Missing, Float64}}(rand(10)),
y = Vector{Union{Missing, String}}(rand(["a", "b", "c"], 10)))
df[[1,4,5], :x] = missing
df[[9,10], :y] = missing
dropmissing(df)
DataFrames.dropmissing!
— Function.Remove rows with missing values in-place.
dropmissing!(df::AbstractDataFrame)
Arguments
df
: the AbstractDataFrame
Result
::AbstractDataFrame
: the updated version
See also dropmissing
and completecases
.
Examples
df = DataFrame(i = 1:10,
x = Vector{Union{Missing, Float64}}(rand(10)),
y = Vector{Union{Missing, String}}(rand(["a", "b", "c"], 10)))
df[[1,4,5], :x] = missing
df[[9,10], :y] = missing
dropmissing!(df)
DataFrames.eachrow
— Function.eachrow(df) => DataFrames.DFRowIterator
Iterate a DataFrame row by row, with each row represented as a DataFrameRow
, which is a view that acts like a one-row DataFrame.
DataFrames.eltypes
— Function.Return element types of columns
eltypes(df::AbstractDataFrame)
Arguments
df
: the AbstractDataFrame
Result
::Vector{Type}
: the element type of each column
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
eltypes(df)
Base.filter
— Function.filter(function, df::AbstractDataFrame)
Return a copy of data frame df
containing only rows for which function
returns true
. The function is passed a DataFrameRow
as its only argument.
Examples
julia> df = DataFrame(x = [3, 1, 2, 1], y = ["b", "c", "a", "b"])
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 1 │ c │
│ 3 │ 2 │ a │
│ 4 │ 1 │ b │
julia> filter(row -> row[:x] > 1, df)
2×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 2 │ a │
Base.filter!
— Function.filter!(function, df::AbstractDataFrame)
Remove rows from data frame df
for which function
returns false
. The function is passed a DataFrameRow
as its only argument.
Examples
julia> df = DataFrame(x = [3, 1, 2, 1], y = ["b", "c", "a", "b"])
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 1 │ c │
│ 3 │ 2 │ a │
│ 4 │ 1 │ b │
julia> filter!(row -> row[:x] > 1, df);
julia> df
2×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 2 │ a │
DataFrames.head
— Function.Show the first or last part of an AbstractDataFrame
head(df::AbstractDataFrame, r::Int = 6)
tail(df::AbstractDataFrame, r::Int = 6)
Arguments
df
: the AbstractDataFramer
: the number of rows to show
Result
::AbstractDataFrame
: the first or last part ofdf
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
head(df)
tail(df)
DataFrames.names!
— Function.Set column names
names!(df::AbstractDataFrame, vals)
Arguments
df
: the AbstractDataFramevals
: column names, normally a Vector{Symbol} the same length as the number of columns indf
makeunique
: iffalse
(the default), an error will be raised if duplicate names are found; iftrue
, duplicate names will be suffixed with_i
(i
starting at 1 for the first duplicate).
Result
::AbstractDataFrame
: the updated result
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
names!(df, [:a, :b, :c])
names!(df, [:a, :b, :a]) # throws ArgumentError
names!(df, [:a, :b, :a], makeunique=true) # renames second :a to :a_1
DataFrames.nonunique
— Function.Indexes of duplicate rows (a row that is a duplicate of a prior row)
nonunique(df::AbstractDataFrame)
nonunique(df::AbstractDataFrame, cols)
Arguments
df
: the AbstractDataFramecols
: a column indicator (Symbol, Int, Vector{Symbol}, etc.) specifying the column(s) to compare
Result
::Vector{Bool}
: indicates whether the row is a duplicate of some prior row
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
df = vcat(df, df)
nonunique(df)
nonunique(df, 1)
DataFrames.rename!
— Function.Rename columns
rename!(df::AbstractDataFrame, (from => to)::Pair{Symbol, Symbol}...)
rename!(df::AbstractDataFrame, d::AbstractDict{Symbol,Symbol})
rename!(df::AbstractDataFrame, d::AbstractArray{Pair{Symbol,Symbol}})
rename!(f::Function, df::AbstractDataFrame)
rename(df::AbstractDataFrame, (from => to)::Pair{Symbol, Symbol}...)
rename(df::AbstractDataFrame, d::AbstractDict{Symbol,Symbol})
rename(df::AbstractDataFrame, d::AbstractArray{Pair{Symbol,Symbol}})
rename(f::Function, df::AbstractDataFrame)
Arguments
df
: the AbstractDataFramed
: an Associative type or an AbstractArray of pairs that maps the original names to new namesf
: a function which for each column takes the old name (a Symbol) and returns the new name (a Symbol)
Result
::AbstractDataFrame
: the updated result
New names are processed sequentially. A new name must not already exist in the DataFrame
at the moment an attempt to rename a column is performed.
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
rename(df, :i => :A, :x => :X)
rename(df, [:i => :A, :x => :X])
rename(df, Dict(:i => :A, :x => :X))
rename(x -> Symbol(uppercase(string(x))), df)
rename!(df, Dict(:i =>: A, :x => :X))
DataFrames.rename
— Function.Rename columns
rename!(df::AbstractDataFrame, (from => to)::Pair{Symbol, Symbol}...)
rename!(df::AbstractDataFrame, d::AbstractDict{Symbol,Symbol})
rename!(df::AbstractDataFrame, d::AbstractArray{Pair{Symbol,Symbol}})
rename!(f::Function, df::AbstractDataFrame)
rename(df::AbstractDataFrame, (from => to)::Pair{Symbol, Symbol}...)
rename(df::AbstractDataFrame, d::AbstractDict{Symbol,Symbol})
rename(df::AbstractDataFrame, d::AbstractArray{Pair{Symbol,Symbol}})
rename(f::Function, df::AbstractDataFrame)
Arguments
df
: the AbstractDataFramed
: an Associative type or an AbstractArray of pairs that maps the original names to new namesf
: a function which for each column takes the old name (a Symbol) and returns the new name (a Symbol)
Result
::AbstractDataFrame
: the updated result
New names are processed sequentially. A new name must not already exist in the DataFrame
at the moment an attempt to rename a column is performed.
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
rename(df, :i => :A, :x => :X)
rename(df, [:i => :A, :x => :X])
rename(df, Dict(:i => :A, :x => :X))
rename(x -> Symbol(uppercase(string(x))), df)
rename!(df, Dict(:i =>: A, :x => :X))
Base.show
— Function.show([io::IO,] df::AbstractDataFrame;
allrows::Bool = !get(io, :limit, false),
allcols::Bool = !get(io, :limit, false),
allgroups::Bool = !get(io, :limit, false),
splitcols::Bool = get(io, :limit, false),
rowlabel::Symbol = :Row,
summary::Bool = true)
Render a data frame to an I/O stream. The specific visual representation chosen depends on the width of the display.
If io
is omitted, the result is printed to stdout
, and allrows
, allcols
and allgroups
default to false
while splitcols
defaults to true
.
Arguments
io::IO
: The I/O stream to whichdf
will be printed.df::AbstractDataFrame
: The data frame to print.allrows::Bool
: Whether to print all rows, rather than a subset that fits the device height. By default this is the case only ifio
does not have theIOContext
propertylimit
set.allcols::Bool
: Whether to print all columns, rather than a subset that fits the device width. By default this is the case only ifio
does not have theIOContext
propertylimit
set.allgroups::Bool
: Whether to print all groups rather than the first and last, whendf
is aGroupedDataFrame
. By default this is the case only ifio
does not have theIOContext
propertylimit
set.splitcols::Bool
: Whether to split printing in chunks of columns fitting the screen width rather than printing all columns in the same block. Only applies ifallcols
istrue
. By default this is the case only ifio
has theIOContext
propertylimit
set.rowlabel::Symbol = :Row
: The label to use for the column containing row numbers.summary::Bool = true
: Whether to print a brief string summary of the data frame.
Examples
julia> using DataFrames
julia> df = DataFrame(A = 1:3, B = ["x", "y", "z"]);
julia> show(df, allcols=true)
3×2 DataFrame
│ Row │ A │ B │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ x │
│ 2 │ 2 │ y │
│ 3 │ 3 │ z │
Base.sort
— Function.sort(df::AbstractDataFrame, cols;
alg::Union{Algorithm, Nothing}=nothing, lt=isless, by=identity,
rev::Bool=false, order::Ordering=Forward)
Return a copy of data frame df
sorted by column(s) cols
. cols
can be either a Symbol
or Integer
column index, or a tuple or vector of such indices.
If alg
is nothing
(the default), the most appropriate algorithm is chosen automatically among TimSort
, MergeSort
and RadixSort
depending on the type of the sorting columns and on the number of rows in df
. If rev
is true
, reverse sorting is performed. To enable reverse sorting only for some columns, pass order(c, rev=true)
in cols
, with c
the corresponding column index (see example below). See sort!
for a description of other keyword arguments.
Examples
julia> df = DataFrame(x = [3, 1, 2, 1], y = ["b", "c", "a", "b"])
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 1 │ c │
│ 3 │ 2 │ a │
│ 4 │ 1 │ b │
julia> sort(df, :x)
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ c │
│ 2 │ 1 │ b │
│ 3 │ 2 │ a │
│ 4 │ 3 │ b │
julia> sort(df, (:x, :y))
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ b │
│ 2 │ 1 │ c │
│ 3 │ 2 │ a │
│ 4 │ 3 │ b │
julia> sort(df, (:x, :y), rev=true)
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 2 │ a │
│ 3 │ 1 │ c │
│ 4 │ 1 │ b │
julia> sort(df, (:x, order(:y, rev=true)))
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ c │
│ 2 │ 1 │ b │
│ 3 │ 2 │ a │
│ 4 │ 3 │ b │
Base.sort!
— Function.sort!(df::AbstractDataFrame, cols;
alg::Union{Algorithm, Nothing}=nothing, lt=isless, by=identity,
rev::Bool=false, order::Ordering=Forward)
Sort data frame df
by column(s) cols
. cols
can be either a Symbol
or Integer
column index, or a tuple or vector of such indices.
If alg
is nothing
(the default), the most appropriate algorithm is chosen automatically among TimSort
, MergeSort
and RadixSort
depending on the type of the sorting columns and on the number of rows in df
. If rev
is true
, reverse sorting is performed. To enable reverse sorting only for some columns, pass order(c, rev=true)
in cols
, with c
the corresponding column index (see example below). See other methods for a description of other keyword arguments.
Examples
julia> df = DataFrame(x = [3, 1, 2, 1], y = ["b", "c", "a", "b"])
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 1 │ c │
│ 3 │ 2 │ a │
│ 4 │ 1 │ b │
julia> sort!(df, :x)
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ c │
│ 2 │ 1 │ b │
│ 3 │ 2 │ a │
│ 4 │ 3 │ b │
julia> sort!(df, (:x, :y))
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ b │
│ 2 │ 1 │ c │
│ 3 │ 2 │ a │
│ 4 │ 3 │ b │
julia> sort!(df, (:x, :y), rev=true)
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ b │
│ 2 │ 2 │ a │
│ 3 │ 1 │ c │
│ 4 │ 1 │ b │
julia> sort!(df, (:x, order(:y, rev=true)))
4×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ c │
│ 2 │ 1 │ b │
│ 3 │ 2 │ a │
│ 4 │ 3 │ b │
DataFrames.tail
— Function.Show the first or last part of an AbstractDataFrame
head(df::AbstractDataFrame, r::Int = 6)
tail(df::AbstractDataFrame, r::Int = 6)
Arguments
df
: the AbstractDataFramer
: the number of rows to show
Result
::AbstractDataFrame
: the first or last part ofdf
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
head(df)
tail(df)
Base.unique!
— Function.Delete duplicate rows
unique(df::AbstractDataFrame)
unique(df::AbstractDataFrame, cols)
unique!(df::AbstractDataFrame)
unique!(df::AbstractDataFrame, cols)
Arguments
df
: the AbstractDataFramecols
: column indicator (Symbol, Int, Vector{Symbol}, etc.)
specifying the column(s) to compare.
Result
::AbstractDataFrame
: the updated version ofdf
with unique rows.
When cols
is specified, the return DataFrame contains complete rows, retaining in each case the first instance for which df[cols]
is unique.
See also nonunique
.
Examples
df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10))
df = vcat(df, df)
unique(df) # doesn't modify df
unique(df, 1)
unique!(df) # modifies df
DataFrames.permutecols!
— Function.permutecols!(df::DataFrame, p::AbstractVector)
Permute the columns of df
in-place, according to permutation p
. Elements of p
may be either column indices (Int
) or names (Symbol
), but cannot be a combination of both. All columns must be listed.
Examples
julia> df = DataFrame(a=1:5, b=2:6, c=3:7)
5×3 DataFrame
│ Row │ a │ b │ c │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 1 │ 2 │ 3 │
│ 2 │ 2 │ 3 │ 4 │
│ 3 │ 3 │ 4 │ 5 │
│ 4 │ 4 │ 5 │ 6 │
│ 5 │ 5 │ 6 │ 7 │
julia> permutecols!(df, [2, 1, 3]);
julia> df
5×3 DataFrame
│ Row │ b │ a │ c │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 2 │ 1 │ 3 │
│ 2 │ 3 │ 2 │ 4 │
│ 3 │ 4 │ 3 │ 5 │
│ 4 │ 5 │ 4 │ 6 │
│ 5 │ 6 │ 5 │ 7 │
julia> permutecols!(df, [:c, :a, :b]);
julia> df
5×3 DataFrame
│ Row │ c │ a │ b │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 3 │ 1 │ 2 │
│ 2 │ 4 │ 2 │ 3 │
│ 3 │ 5 │ 3 │ 4 │
│ 4 │ 6 │ 4 │ 5 │
│ 5 │ 7 │ 5 │ 6 │