使用DataFrame之前先导入
using DataFrames # load package 注意大小写
先来创建一个DataFrame
对象x
x=DataFrame(A=[1,2],B=[1.0,missing],C=["A","B"])
#=
2×3 DataFrame
│ Row │ A │ B │ C │
│ │ Int64 │ Float64⍰ │ String │
├─────┼───────┼──────────┼────────┤
│ 1 │ 1 │ 1.0 │ A │
│ 2 │ 2 │ missing │ B │
=#
使用size
函数获取DataFrame
大小
julia> size(x)
(2, 3)
julia> size(x,1)
2
julia> size(x,2)
3
使用nrow``ncol
分别获取行数跟列数,使用length
可以获取列数,但不推荐这么做
julia> nrow(x)
2
julia> ncol(x)
3
julia> length(x)
┌ Warning: `length(df::AbstractDataFrame)` is deprecated, use `size(df, 2)` instead.
│ caller = top-level scope at none:0
└ @ Core none:0
3
使用describe获取DataFrame的主要信息
# 先输出
julia> x
2×3 DataFrame
│ Row │ A │ B │ C │
│ │ Int64 │ Float64⍰ │ String │
├─────┼───────┼──────────┼────────┤
│ 1 │ 1 │ 1.0 │ A │
│ 2 │ 2 │ missing │ B │
julia> describe(x)
3×8 DataFrame
│ Row │ variable │ mean │ min │ median │ max │ nunique │ nmissing │ eltype │
│ │ Symbol │ Union… │ Any │ Union… │ Any │ Union… │ Union… │ DataType │
├─────┼──────────┼────────┼─────┼────────┼─────┼─────────┼──────────┼──────────┤
│ 1 │ A │ 1.5 │ 1 │ 1.5 │ 2 │ │ │ Int64 │
│ 2 │ B │ 1.0 │ 1.0 │ 1.0 │ 1.0 │ │ 1 │ Float64 │
│ 3 │ C │ │ A │ │ B │ 2 │ │ String │
使用showcols
将获取的信息存入数据帧中
julia> showcols(x)
┌ Warning: `showcols(df::AbstractDataFrame, all::Bool=false, values::Bool=true)` is deprecated, use `describe(df, stats=[:eltype, :nmissing, :first, :last])` instead.
│ caller = showcols(::DataFrame) at deprecated.jl:54
└ @ DataFrames ./deprecated.jl:54
3×5 DataFrame
│ Row │ variable │ eltype │ nmissing │ first │ last │
│ │ Symbol │ DataType │ Union… │ Any │ Any │
├─────┼──────────┼──────────┼──────────┼───────┼─────────┤
│ 1 │ A │ Int64 │ │ 1 │ 2 │
│ 2 │ B │ Float64 │ 1 │ 1.0 │ missing │
│ 3 │ C │ String │ │ A │ B │
names
将返回所有列名
julia> names(x)
3-element Array{Symbol,1}:
:A
:B
:C
eltypes
将返回所有的数据类型
julia> eltypes(x)
3-element Array{Type,1}:
Int64
Union{Missing, Float64}
String
接下来创建一个比较大的DataFrame
y=DataFrame(rand(1:10,1000,10))
#=
julia> y=DataFrame(rand(1:10,1000,10))
1000×10 DataFrame
│ Row │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├──────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 8 │ 3 │ 7 │ 3 │ 1 │ 9 │ 4 │ 1 │ 7 │ 1 │
│ 2 │ 2 │ 7 │ 2 │ 3 │ 7 │ 10 │ 4 │ 4 │ 8 │ 5 │
│ 3 │ 9 │ 3 │ 6 │ 6 │ 9 │ 1 │ 6 │ 5 │ 5 │ 10 │
│ 4 │ 7 │ 10 │ 1 │ 5 │ 9 │ 2 │ 10 │ 7 │ 9 │ 6 │
│ 5 │ 4 │ 6 │ 5 │ 10 │ 1 │ 5 │ 5 │ 6 │ 1 │ 5 │
│ 6 │ 5 │ 9 │ 2 │ 4 │ 9 │ 1 │ 8 │ 1 │ 1 │ 2 │
│ 7 │ 9 │ 6 │ 2 │ 4 │ 5 │ 2 │ 7 │ 9 │ 10 │ 6 │
│ 8 │ 3 │ 9 │ 3 │ 10 │ 3 │ 9 │ 1 │ 9 │ 7 │ 4 │
│ 9 │ 3 │ 2 │ 6 │ 5 │ 1 │ 5 │ 9 │ 3 │ 2 │ 4 │
│ 10 │ 2 │ 1 │ 1 │ 4 │ 4 │ 5 │ 6 │ 2 │ 1 │ 1 │
⋮
│ 990 │ 4 │ 3 │ 6 │ 6 │ 10 │ 2 │ 1 │ 4 │ 4 │ 10 │
│ 991 │ 6 │ 2 │ 8 │ 3 │ 7 │ 9 │ 7 │ 7 │ 5 │ 3 │
│ 992 │ 4 │ 9 │ 6 │ 8 │ 6 │ 8 │ 1 │ 4 │ 10 │ 9 │
│ 993 │ 4 │ 9 │ 2 │ 5 │ 4 │ 1 │ 8 │ 8 │ 3 │ 7 │
│ 994 │ 8 │ 2 │ 4 │ 1 │ 3 │ 7 │ 4 │ 5 │ 3 │ 7 │
│ 995 │ 1 │ 7 │ 9 │ 5 │ 2 │ 9 │ 3 │ 8 │ 4 │ 6 │
│ 996 │ 1 │ 4 │ 8 │ 1 │ 2 │ 5 │ 6 │ 2 │ 3 │ 4 │
│ 997 │ 3 │ 4 │ 3 │ 7 │ 7 │ 7 │ 5 │ 4 │ 7 │ 8 │
│ 998 │ 10 │ 1 │ 5 │ 4 │ 4 │ 7 │ 9 │ 8 │ 2 │ 1 │
│ 999 │ 8 │ 7 │ 2 │ 4 │ 6 │ 6 │ 1 │ 1 │ 5 │ 6 │
│ 1000 │ 1 │ 10 │ 4 │ 7 │ 10 │ 1 │ 4 │ 1 │ 9 │ 7 │
=#
接下来使用head
获取顶部的几行
head(y)
#=
julia> head(y)
┌ Warning: `head(df::AbstractDataFrame)` is deprecated, use `first(df, 6)` instead.
│ caller = top-level scope at none:0
└ @ Core none:0
6×10 DataFrame
│ Row │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 8 │ 3 │ 7 │ 3 │ 1 │ 9 │ 4 │ 1 │ 7 │ 1 │
│ 2 │ 2 │ 7 │ 2 │ 3 │ 7 │ 10 │ 4 │ 4 │ 8 │ 5 │
│ 3 │ 9 │ 3 │ 6 │ 6 │ 9 │ 1 │ 6 │ 5 │ 5 │ 10 │
│ 4 │ 7 │ 10 │ 1 │ 5 │ 9 │ 2 │ 10 │ 7 │ 9 │ 6 │
│ 5 │ 4 │ 6 │ 5 │ 10 │ 1 │ 5 │ 5 │ 6 │ 1 │ 5 │
│ 6 │ 5 │ 9 │ 2 │ 4 │ 9 │ 1 │ 8 │ 1 │ 1 │ 2 │
=#
使用tail
函数可以获取你想获取的前几行
# 这里我获取前三行
julia> tail(y,3)
┌ Warning: `tail(df::AbstractDataFrame, n::Integer)` is deprecated, use `last(df, n)` instead.
│ caller = top-level scope at none:0
└ @ Core none:0
3×10 DataFrame
│ Row │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 10 │ 1 │ 5 │ 4 │ 4 │ 7 │ 9 │ 8 │ 2 │ 1 │
│ 2 │ 8 │ 7 │ 2 │ 4 │ 6 │ 6 │ 1 │ 1 │ 5 │ 6 │
│ 3 │ 1 │ 10 │ 4 │ 7 │ 10 │ 1 │ 4 │ 1 │ 9 │ 7 │
介绍一些最基本的值获取和设置值
给你一个DataFrame``x
,有三种方法可以将其中的某一列转换为一个数组
先输出x
julia> x
2×3 DataFrame
│ Row │ A │ B │ C │
│ │ Int64 │ Float64⍰ │ String │
├─────┼───────┼──────────┼────────┤
│ 1 │ 1 │ 1.0 │ A │
│ 2 │ 2 │ missing │ B │
julia> x[1]
2-element Array{Int64,1}:
1
2
julia> x[:A]
2-element Array{Int64,1}:
1
2
julia> x[:1]
2-element Array{Int64,1}:
1
2
可以利用索引来获取某一行
julia> x[1,:]
DataFrameRow (row 1)
A 1
B 1.0
C A
可以使用相同的语法来获取一个具体的值
julia> x[1,1]
1
使用同一标量
julia> x[1:2,1:2]=1
1
julia> x
2×3 DataFrame
│ Row │ A │ B │ C │
│ │ Int64 │ Float64⍰ │ String │
├─────┼───────┼──────────┼────────┤
│ 1 │ 1 │ 1.0 │ A │
│ 2 │ 1 │ 1.0 │ B │
使用同等大小的数据集来进行替换
julia> x[1:2,1:2]=DataFrame([5 6;7 8])
2×2 DataFrame
│ Row │ x1 │ x2 │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 5 │ 6 │
│ 2 │ 7 │ 8 │
julia> x
2×3 DataFrame
│ Row │ A │ B │ C │
│ │ Int64 │ Float64⍰ │ String │
├─────┼───────┼──────────┼────────┤
│ 1 │ 5 │ 6.0 │ A │
│ 2 │ 7 │ 8.0 │ B │
差不多了吧,再想起来再补
先写到这儿,记得点赞,关注,记得订阅 Julia Code 专栏!!!