Returning the n lowest values across columns in a dataframe

This works, but there must be a better one than this:

n_lowest(row::DataFrameRow,n::Integer) = row[[1; partialsortperm(Vector(row[2:end]), 1:n).+1]]

n_lowest.(eachrow(df),3)

This is specific to your case where you have a “name” column in the first position, so we need an ‘offset’ index [[1; and the .+1 shift.

1 Like