Julia - dataframe - How to use string for custom output column naming in by()

倖福魔咒の 提交于 2020-02-25 02:11:10

问题


I am getting familiar with Julia Dataframes module. One thing that I haven't found a way yet to do is how to assign programmatically a custom column name of the result of a by() operation.

So for example I have no problem doing the following :

df = DataFrame(grp = rand(["a","b"], 100), x= rand(100), y = rand(100), z=rand(100))
by(df, :grp,result=(:x, :z)=>((x, y),) -> cov(x, y))

Giving the following dataframe

2×2 DataFrame
│ Row │ grp    │ result      │
│     │ String │ Float64     │
├─────┼────────┼─────────────┤
│ 1   │ b      │ -0.00622699 │
│ 2   │ a      │ -0.0303828  │

Now I would like the naming of the result to be dependent on some other part of my code. So i am trying things along this

resultColName="resultBis"
by(df, :grp,resultColName=(:x, :z)=>((x, y),) -> cov(x, y))

which gives me the following

2×2 DataFrame
│ Row │ grp    │ resultColName │
│     │ String │ Float64       │
├─────┼────────┼───────────────┤
│ 1   │ b      │ -0.00622699   │
│ 2   │ a      │ -0.0303828    │

Which doesn't work as I want the result column to be named 'resultBis'. I understand why this happens, but is there currently a way to provide a string to chose the custom name of the result column ?

I imagine using macro could be one way to handle that and I would welcome that as an answer, but ideally I would like to do it directly within the DataFrames.jl. Any help is welcome. Thanks


回答1:


julia> by(df, :grp, (; Symbol(resultColName)=>(:x, :z)=>((x, y),) -> cov(x, y)))
2×2 DataFrame
│ Row │ grp    │ resultBis  │
│     │ String │ Float64    │
├─────┼────────┼────────────┤
│ 1   │ a      │ -0.0110717 │
│ 2   │ b      │ 0.0102181  │

Explanation:

by accepts a NamedTuple as the third parameter. In order to create it programmatically we use the (; :key => value) operator. For more information type ?NamedTuple into the Julia console.



来源:https://stackoverflow.com/questions/60229490/julia-dataframe-how-to-use-string-for-custom-output-column-naming-in-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!