# DataFrame
具有命名变量的表数组(变量可包含不同类型的数据)
函数库: TyBase
# 语法
DataFrame(pairs::Pair...; makeunique::Bool=false, copycols::Bool=true)
DataFrame(pairs::AbstractVector{<:Pair}; makeunique::Bool=false, copycols::Bool=true)
DataFrame(ds::AbstractDict; copycols::Bool=true)
DataFrame(kwargs..., copycols::Bool=true)
DataFrame(columns::AbstractVecOrMat,
names::AbstractVector;
makeunique::Bool=false, copycols::Bool=true)
DataFrame(table; copycols::Union{Bool, Nothing}=nothing)
DataFrame(::DataFrameRow)
DataFrame(::GroupedDataFrame; keepkeys::Bool=true)
# 说明
DataFrame(pairs::Pair...; makeunique::Bool=false, copycols::Bool=true),DataFrame(pairs::AbstractVector{<:Pair}; makeunique::Bool=false, copycols::Bool=true) 根据键值对pairs创建表格。 示例
DataFrame(ds::AbstractDict; copycols::Bool=true) 通过字典ds创建表格。示例
DataFrame(kwargs..., copycols::Bool=true)通过输入变量及对应数值等信息创建表格。示例
DataFrame(columns::AbstractVecOrMat,names::AbstractVectormakeunique::Bool=false, copycols::Bool=true) 通过直接指定变量与列数据创建表格。示例
DataFrame(table; copycols::Union{Bool, Nothing}=nothing)、DataFrame(::DataFrameRow)、DataFrame(::GroupedDataFrame; keepkeys::Bool=true)将单行表、分类表等提升为DataFrame类型。示例
# 示例
创建表格
- 直接对定义表格变量和数据
using TyBase
DataFrame((a = [1,2],b = [3,4]))
ans = 2×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 3
2 │ 2 4
DataFrame(A=1:3, B=5:7, fixed=1)
ans = 3×3 DataFrame
Row │ A B fixed
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 1 5 1
2 │ 2 6 1
3 │ 3 7 1
- 使用字典(键值对)创建表格
DataFrame("a" => 1:2, "b" => 0)
ans = 2×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 0
2 │ 2 0
DataFrame([:a => 1:2, :b => 0])
ans = 2×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 0
2 │ 2 0
- 用矩阵创建DataFrame。将矩阵作为第一个参数,第二个参数使用 :auto 将会自动创建x1,x2,...
DataFrame([1 0; 2 0], :auto)
ans = 2×2 DataFrame
Row │ x1 x2
│ Int64 Int64
─────┼──────────────
1 │ 1 0
2 │ 2 0
改变类型为DataFrame
将单行表提升为DataFrame
先创建一个表格并提取某一行
using TyBase
df = DataFrame(a=1:3, b=4:6)
dfr = df[1,:]
dfr = DataFrameRow
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 4
可以看到dfr类型为DataFrameRow。使用DataFrame函数将其转变为DataFrame类型
DataFrame(dfr)
ans = 1×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 4
将分组表提升为DataFrame
先创建一个表格并按:a进行分组
df = DataFrame(a=repeat([1, 2, 3, 4], outer=[2]),
b=repeat([2, 1], outer=[4]),c=1:8)
gd = groupby(df,:a)
gd = GroupedDataFrame with 4 groups based on key: a
First Group (2 rows): a = 1
Row │ a b c
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 1 2 1
2 │ 1 2 5
⋮
Last Group (2 rows): a = 4
Row │ a b c
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 4 1 4
2 │ 4 1 8
转变gd类型
DataFrame(gd)
ans = 8×3 DataFrame
Row │ a b c
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 1 2 1
2 │ 1 2 5
3 │ 2 1 2
4 │ 2 1 6
5 │ 3 2 3
6 │ 3 2 7
7 │ 4 1 4
8 │ 4 1 8
# 输入参数
pairs - 键值对键值对或键值对向量
输入键值对或键值对向量构建表格,键值对指定键为Symbol或字符串类型,值为任意元素。
类似的,也可以输入字典,键指定为Symbol或字符串类型
示例:DataFrame(:a=>[1,2,3],:b=>[4,5,6])
示例:DataFrame(Dict(:a=>[1,2,3],:b=>[4,5,6]))
# 详细信息
- makeunique:如果为false(默认),如果在未加入的列名中发现重复名称,则会引发错误
- copycols:是否应复制作为列传递的向量; 默认设置为 true 并复制向量;