手动设置bin尺寸,用于数据框架中的分组直方图箱

发布于 2025-01-24 16:58:37 字数 912 浏览 4 评论 0原文

我一直在使用以下代码来生成直方图,并使用该箱的一列并使用该箱来计算另一列的中位数。

using Plots, Statistics, DataFrames
df = DataFrame(x=repeat(["a","b","c"], 5), y=1:15)
res = combine(groupby(df, :x, sort=true), :y => median)
bar(res.x, res.y_median, legend=false)

代码点选择垃圾箱的值,如果可能的话,我想手动应用一个值范围的值?

Row  │ A        B_median 
     │ Any      Float64  
─────┼───────────────────
   1 │ 1515.74     0.09
   2 │ 1517.7      0.81
   3 │ 1527.22    10.23
   4 │ 1529.88     2.95
   5 │ 1530.72    17.32
   6 │ 1530.86    15.22
   7 │ 1532.26     1.45
   8 │ 1532.68    18.51
   9 │ 1541.08     1.32
  10 │ 1541.22    15.78
  11 │ 1541.36     0.12
  12 │ 1541.5     13.55
  13 │ 1541.92    11.99
  14 │ 1542.06    21.14
  15 │ 1542.34    10.645
  16 │ 1542.62    19.95
  17 │ 1542.76    21.0
  18 │ 1543.32    20.91

例如,而不是单独计算9-> 17行的中位数。这些行能否自动堆在一起,即1542.7 +/- 0.7,并为此范围计算总价值吗? 非常感谢!

I've been using the following code to generate histograms from binning one column of a Dataframe and using that bin to calculate a median from another column.

using Plots, Statistics, DataFrames
df = DataFrame(x=repeat(["a","b","c"], 5), y=1:15)
res = combine(groupby(df, :x, sort=true), :y => median)
bar(res.x, res.y_median, legend=false)

The code point selects values for the bins and I would like to apply a bin range of values manually, if possible?

Row  │ A        B_median 
     │ Any      Float64  
─────┼───────────────────
   1 │ 1515.74     0.09
   2 │ 1517.7      0.81
   3 │ 1527.22    10.23
   4 │ 1529.88     2.95
   5 │ 1530.72    17.32
   6 │ 1530.86    15.22
   7 │ 1532.26     1.45
   8 │ 1532.68    18.51
   9 │ 1541.08     1.32
  10 │ 1541.22    15.78
  11 │ 1541.36     0.12
  12 │ 1541.5     13.55
  13 │ 1541.92    11.99
  14 │ 1542.06    21.14
  15 │ 1542.34    10.645
  16 │ 1542.62    19.95
  17 │ 1542.76    21.0
  18 │ 1543.32    20.91

For example, instead of calculating a median for rows 9->17 individually. Could these rows be bunched together automatically i.e. 1542.7+/-0.7 and a total median value be calculated for this range?
Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撕心裂肺的伤痛 2025-01-31 16:58:37

我认为您想要这样的东西:

julia> using DataFrames, CategoricalArrays, Random, Statistics

julia> Random.seed!(1234);

julia> df = DataFrame(A=rand(20), B=rand(20));

julia> df.A_group = cut(df.A, 4);

julia> res = combine(groupby(df, :A_group, sort=true), :B => median)
4×2 DataFrame
 Row │ A_group                            B_median
     │ Cat…                               Float64
─────┼─────────────────────────────────────────────
   1 │ Q1: [0.014908849285099945, 0.532…  0.134685
   2 │ Q2: [0.5323651749779272, 0.65860…  0.347995
   3 │ Q3: [0.6586057536399257, 0.81493…  0.501756
   4 │ Q4: [0.8149335702852593, 0.97213…  0.531899

I assume you want something like this:

julia> using DataFrames, CategoricalArrays, Random, Statistics

julia> Random.seed!(1234);

julia> df = DataFrame(A=rand(20), B=rand(20));

julia> df.A_group = cut(df.A, 4);

julia> res = combine(groupby(df, :A_group, sort=true), :B => median)
4×2 DataFrame
 Row │ A_group                            B_median
     │ Cat…                               Float64
─────┼─────────────────────────────────────────────
   1 │ Q1: [0.014908849285099945, 0.532…  0.134685
   2 │ Q2: [0.5323651749779272, 0.65860…  0.347995
   3 │ Q3: [0.6586057536399257, 0.81493…  0.501756
   4 │ Q4: [0.8149335702852593, 0.97213…  0.531899
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文