julia - 来自 Julia 中数据框列的向量
问题描述
我有一个DataFrame
df = DataFrame(x = 1:3, y = 4:6)
3×2 DataFrame
Row │ x y
│ Int64 Int64
─────┼──────────────
1 │ 1 4
2 │ 2 5
3 │ 3 6
如何将其中一列提取为Vector
?
我知道我可以做df[:,:x]
or df.x
,但是有没有办法用函数来做呢?我问的原因是我使用这个Chain.jl
包并且想做类似的事情
@chain df begin
some_manipulations_here
pull(:x)
end
解决方案
您可以执行以下操作之一:
julia> df = DataFrame(x = 1:3, y = 4:6)
3×2 DataFrame
Row │ x y
│ Int64 Int64
─────┼──────────────
1 │ 1 4
2 │ 2 5
3 │ 3 6
julia> @chain df begin
_.x
end
3-element Vector{Int64}:
1
2
3
julia> @chain df begin
getproperty(:x) # same as above
end
3-element Vector{Int64}:
1
2
3
julia> @chain df begin
getindex(!, :x) # also _[!, :x]
end
3-element Vector{Int64}:
1
2
3
julia> @chain df begin
getindex(:, :x) # also _[:, :x]
end
3-element Vector{Int64}:
1
2
3
可能是第一个选项(_.x
在实践中是最简单的。
我已经展示了其他选项来强调所有特殊语法,例如df.x
ordf[!, :x]
实际上是函数调用,特殊语法只是为了方便。
推荐阅读
- javascript - 如何使用过滤器方法从数组中返回元素?
- python - 即使在 matplotlib 中的 plt.cla() 之后,如何强制 xlim 永久不变
- javascript - 如何在其实例中读取 ES6 类的静态属性?
- excel - 条件格式不更新表中的应用程序区域(列表对象)
- python - 如何有效地创建嵌套循环并附加所有循环的输出(作为多维数组)?
- java - System.getproperty("spring.profiles.active") 总是在 JPA 实体监听器中获取 Null
- angular - 在 prodMode 中更改检测角度
- azure-resource-manager - 将虚拟机加入域
- artifactory - Artifcatory REST API 列出所有存储库
- java - 在另一个表中使用复合外键映射列数据