我正在使用来自DataFramesMeta.jl,的宏,并且我希望将列名存储在一个变量中。以下是我的输入数据:
using DataFrames
using DataFramesMeta
using Statistics
df = DataFrame(g = ["a", "a", "b", "b"], x = 1:4)
gdf = groupby(df, :g)
colname = :x
如果我在@select
中使用colname
,它在某种程度上是有效的,除了它返回一个数组而不是数据帧:
julia> @select(df, :x)
4×1 DataFrame
│ Row │ x │
│ │ Int64 │
├─────┼───────┤
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 3 │
│ 4 │ 4 │
julia> @select(df, colname)
4-element Array{Int64,1}:
1
2
3
4
如果我试图在@where
或@group_by
中使用colname
,他们会抛出方法错误:
julia> @where(df, :x .> 2)
2×2 DataFrame
│ Row │ g │ x │
│ │ String │ Int64 │
├─────┼────────┼───────┤
│ 1 │ b │ 3 │
│ 2 │ b │ 4 │
julia> @where(df, colname .> 2)
ERROR: MethodError: no method matching isless(::Int64, ::Symbol)
julia> @based_on(gdf, xavg = mean(:x))
2×2 DataFrame
│ Row │ g │ xavg │
│ │ String │ Float64 │
├─────┼────────┼─────────┤
│ 1 │ a │ 1.5 │
│ 2 │ b │ 3.5 │
julia> @based_on(gdf, xavg = mean(colname))
ERROR: MethodError: no method matching iterate(::Symbol)
有没有办法在DataFramesMeta.jl宏中将列名作为变量传递?
发布于 2019-04-17 04:01:37
https://stackoverflow.com/questions/55715518
复制相似问题