Setting multiple columns in DataFrames


#1

Is there a one-line way to set multiple columns of DataFrame row?
Seems like a trivial question, but can’t find a way to do it.

Suppose I have:

df = DataFrame(a = [5, 6, 7], b = [55, 66, 77])

The following and any similar approach I try throws an error

df[1, :] = [999, 888]

ERROR: MethodError: Cannot `convert` an object of type Array{Int64,1} to an object of type Int64 This may have arisen from a call to the constructor Int64(...), since type constructors fall back to convert methods.

Thanks!


#2

One way is just to define it:

julia> setrow!(df::DataFrame,row::Int,val::Vector) = ( foreach(i->df[row,i]=val[i],1:length(df)) ; df )
setrow! (generic function with 1 method)

julia> setrow!(df,1,[999,888])
3×2 DataFrames.DataFrame
│ Row │ a   │ b   │
├─────┼─────┼─────┤
│ 1   │ 999 │ 888 │
│ 2   │ 6   │ 66  │
│ 3   │ 7   │ 77  │