Julia Dataframe group by inside another group by

Viewed 208

i have a dataframe like the following :

julia> DataFrame(val=1:10,  percent=nothing)
10×2 DataFrame
 Row │ val    percent
     │ Int64  Nothing
─────┼────────────────
   1 │     1
   2 │     2
   3 │     3
   4 │     4
   5 │     5
   6 │     6
   7 │     7
   8 │     8
   9 │     9
  10 │    10

i want to apply this :

percent(df, threshold=0.33) = df / sum(df) .> threshold

which calculate the percentage and check if it's above threshold of a each value in a column compared with the total of the same column

to a DataFrame grouped by two times. i grouped it by USER_KEY and then i want to group by again for each other column and then combine / apply the percent function to each. It doesnt work i get

ERROR: MethodError: no method matching combine(::GroupedDataFrame{DataFrame}, ::var"#64#65")

i don't understand this error ..., If someone can help thank you very much

EDIT : There is a little difference with this example and i don't know how to reproduce it easily , it's that with these 2 columns i also have a column user_key where some keys can have many lines , i want to group by user_key and then group by val .

I want the column percent to have the percentage of the total of the column val so for this dataframe the total is 10 i want the result to be like that :

10×2 DataFrame
 Row │ val    percent
     │ Int64  Float64
─────┼────────────────
   1 │     1. 0.1
   2 │     2. 0.2
   3 │     3. 0.3
   4 │     4  0.4
1 Answers

Let me give an answer to the question in the edited part. But probably this is not all that you need - please comment in the question for me to learn what you need more.

So the simplest approach to your problem is:

julia> df = DataFrame(val=1:4)
4×1 DataFrame
 Row │ val   
     │ Int64 
─────┼───────
   1 │     1
   2 │     2
   3 │     3
   4 │     4

julia> df.percent = df.val / sum(df.val)
4-element Array{Float64,1}:
 0.1
 0.2
 0.3
 0.4

julia> df
4×2 DataFrame
 Row │ val    percent 
     │ Int64  Float64 
─────┼────────────────
   1 │     1      0.1
   2 │     2      0.2
   3 │     3      0.3
   4 │     4      0.4

alternatively you can use transform!:

julia> df = DataFrame(val=1:4)
4×1 DataFrame
 Row │ val   
     │ Int64 
─────┼───────
   1 │     1
   2 │     2
   3 │     3
   4 │     4

julia> transform!(df, :val => (x -> x / sum(x)) => :percent)
4×2 DataFrame
 Row │ val    percent 
     │ Int64  Float64 
─────┼────────────────
   1 │     1      0.1
   2 │     2      0.2
   3 │     3      0.3
   4 │     4      0.4
Related