dataframe - 如何在 Julia 中将 GroupedDataFrame 转换为 DataFrame?

标签 dataframe julia plots.jl

我使用 groupby 函数对 DataFrame 的子集进行了计算:

using RDatasets
iris = dataset("datasets", "iris")
describe(iris)
iris_grouped = groupby(iris,:Species)
iris_avg = map(:SepalLength => mean,iris_grouped::GroupedDataFrame)

现在我想绘制结果,但我收到以下绘图的错误消息:

@df iris_avg bar(:Species,:SepalLength)

Only tables are supported

绘制数据的最佳方式是什么?我的想法是创建一个单一的 DataFrame 并从那里开始。我该怎么做,即如何将 GroupedDataFrame 转换为单个 DataFrame?谢谢!

最佳答案

要将 GroupedDataFrame 转换为 DataFrame 只需对其调用 DataFrame,例如:

julia> DataFrame(iris_avg)
3×2 DataFrame
│ Row │ Species      │ SepalLength_mean │
│     │ Categorical… │ Float64          │
├─────┼──────────────┼──────────────────┤
│ 1   │ setosa       │ 5.006            │
│ 2   │ versicolor   │ 5.936            │
│ 3   │ virginica    │ 6.588            │

在你的情况下。

你也可以这样写:

julia> combine(:SepalLength => mean, iris_grouped)
3×2 DataFrame
│ Row │ Species      │ SepalLength_mean │
│     │ Categorical… │ Float64          │
├─────┼──────────────┼──────────────────┤
│ 1   │ setosa       │ 5.006            │
│ 2   │ versicolor   │ 5.936            │
│ 3   │ virginica    │ 6.588            │

在原始的GroupedDataFrame

julia> by(:SepalLength => mean, iris, :Species)
3×2 DataFrame
│ Row │ Species      │ SepalLength_mean │
│     │ Categorical… │ Float64          │
├─────┼──────────────┼──────────────────┤
│ 1   │ setosa       │ 5.006            │
│ 2   │ versicolor   │ 5.936            │
│ 3   │ virginica    │ 6.588            │

在原始 DataFrame 上。

我在这里将转换写为第一个参数,但通常,您会将其写为最后一个(这样您就可以传递多个转换),例如:

julia> by(iris, :Species, :SepalLength => mean, :SepalWidth => minimum)
3×3 DataFrame
│ Row │ Species      │ SepalLength_mean │ SepalWidth_minimum │
│     │ Categorical… │ Float64          │ Float64            │
├─────┼──────────────┼──────────────────┼────────────────────┤
│ 1   │ setosa       │ 5.006            │ 2.3                │
│ 2   │ versicolor   │ 5.936            │ 2.0                │
│ 3   │ virginica    │ 6.588            │ 2.2                │

关于dataframe - 如何在 Julia 中将 GroupedDataFrame 转换为 DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066088/

相关文章:

arrays - 将数组内容推送到 DataFrame - Julia

plot - 更改颜色条热图中的中断数量 Julia

julia - Plots.jl 添加趋势线,并更改 x 轴

python - 如何使用嵌套 for 循环和 pandas 的 iloc 来定位具有 1 的行和列

julia - Julia 中是否存在 randsample 函数?

string - Julia - 修剪字符串空格并检查长度

plot - 使用 Julia-Package "Plots"的不对称带状折线图

R:对分组变量的每个成对组合进行 t 检验,对 ID 变量中的每个元素进行

python - 根据分位数值修剪系列/数据框

python - 无法将数据帧列转换为 24 小时格式日期时间