diff --git a/src/DataFrames.jl b/src/DataFrames.jl index 2f54b096b8..31441416f4 100644 --- a/src/DataFrames.jl +++ b/src/DataFrames.jl @@ -68,6 +68,7 @@ export AbstractDataFrame, head, tail, permutecols!, + rowwise, # Remove after deprecation period pool, diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index 32e15b817f..8250bf1d40 100644 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -1105,4 +1105,4 @@ end function permutecols!(df::DataFrame, p::AbstractVector{Symbol}) permutecols!(df, getindex.(index(df).lookup, p)) -end +end \ No newline at end of file diff --git a/src/dataframerow/utils.jl b/src/dataframerow/utils.jl index 9986eaf63e..2276f2216b 100644 --- a/src/dataframerow/utils.jl +++ b/src/dataframerow/utils.jl @@ -218,3 +218,28 @@ function Base.getindex(gd::RowGroupDict, dfr::DataFrameRow) gix = gd.groups[g_row] return view(gd.rperm, gd.starts[gix]:gd.stops[gix]) end + +""" + rowwise(f::Function, df::DataFrame) + +Performs a function alonw an array from each row in a DataFrame, +returning an array of length `nrow(df)` + +```julia +julia> df = DataFrame(a = [2,200], b = [4,400]) +2×2 DataFrames.DataFrame +│ Row │ a │ b │ +├─────┼─────┼─────┤ +│ 1 │ 2 │ 4 │ +│ 2 │ 200 │ 400 │ + +julia> t = rowwise(mean, df) +2-element Array{Float64,1}: + 3.0 + 300.0 + ``` + +""" +function rowwise(f::Function, df::DataFrame) + t = [f([x[2] for x in collect(row)]) for row in eachrow(df)] +end \ No newline at end of file diff --git a/src/other/utils.jl b/src/other/utils.jl index 8dd76cb075..cfdd5d9ce0 100644 --- a/src/other/utils.jl +++ b/src/other/utils.jl @@ -134,4 +134,4 @@ function _fnames(fs::Vector{T}) where T<:Function name end names -end +end \ No newline at end of file diff --git a/test/dataframerow.jl b/test/dataframerow.jl index af54d66ba2..d2473266f6 100644 --- a/test/dataframerow.jl +++ b/test/dataframerow.jl @@ -74,6 +74,10 @@ module TestDataFrameRow gd = DataFrames.group_rows(df5[1,:]) @test gd.ngroups == 1 + # rowwise operator + df = DataFrame(a = [2,200], b = [4,400]) + @test rowwise(Compat.mean, df) = [3.0, 300] + # getproperty, setproperty! and propertynames if VERSION >= v"0.7.0-DEV.3067" r = DataFrameRow(df, 1)