1

my apologies if this is a simple question but I couldn't find a direct answer on the internet and I think it's a useful problem for others. I'm using Julia and DataFrame and I want to bin on column and then take the median of those bins and plot them in a histogram style plot.

Many thanks if you can help on this!

nboogerz
  • 31
  • 3

1 Answers1

0

This is a basic approach:

julia> using Plots, Statistics, DataFrames

julia> df = DataFrame(x=repeat(["a","b","c"], 5), y=1:15)
15×2 DataFrame
 Row │ x       y
     │ String  Int64
─────┼───────────────
   1 │ a           1
   2 │ b           2
   3 │ c           3
   4 │ a           4
   5 │ b           5
   6 │ c           6
   7 │ a           7
   8 │ b           8
   9 │ c           9
  10 │ a          10
  11 │ b          11
  12 │ c          12
  13 │ a          13
  14 │ b          14
  15 │ c          15

julia> res = combine(groupby(df, :x, sort=true), :y => median)
3×2 DataFrame
 Row │ x       y_median
     │ String  Float64
─────┼──────────────────
   1 │ a            7.0
   2 │ b            8.0
   3 │ c            9.0

julia> bar(res.x, res.y_median, legend=false)

which gives you:

enter image description here

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107