高效工作流

Julia 中的数据操作

Katerina Zahradova

Instructor

命名技巧

  • 名称要短且有意义
    • wages 而非 df
    • wages 而非 us_min_wages_data_between_1968_and_2020_with_inflation_adjusted_column
  • 遵循命名约定/模式
    • 混用 state_wage_2020effective.2020.dollars 难记
    • 大小写也要统一,避免同一 DataFrame 中既有 stateYear、又有 REGION
Julia 中的数据操作

变量太多

  • 不要创建过多新变量

    • 占用内存
    • 混乱:wages_no_missingwages_missing_state_onlywages_original_no_missingwages_state_mean_no_missing 等有何区别?
  • 覆盖写!使用 select!()transform!()

  • chain 宏,减少同一数据的多个版本
Julia 中的数据操作

用变量代替硬编码

  • 用变量替代硬编码数值
# 更推荐
replace_missing = 0

replace!(df.col1, missing => replace_missing)
replace!(df.col2, missing => replace_missing)

# 而不是
replace!(df.col1, missing => 0)
replace!(df.col2, missing => 0)
Julia 中的数据操作

封装成函数

  • 写函数,避免重复写同样的代码
    • 函数可防止笔误
    • 一次设好,后续更快
# 带标签的多折线图函数
function make_line_plot(xs, ys,labels; xlabel="", ylabel="", title="")
    p = plot(title = title, xlabel = xlabel, ylabel = ylabel)
    for (x, y, label) in zip(xs, ys, labels)
        plot!(x, y, label=label)
    end
    p
end
Julia 中的数据操作

注释与文档

  • 用注释说明你在做什么
# 标准化名称
rename!(df, :ColumnOne => :col_1)

# 缺失公司所在的行
df[ismissing.(df.company),:]

# 按年份和州透视
unstack(wages, :year, :state, :eff_min_wage)
  • 记录为何这样做
# 用最小值替换缺失工资
# 作为最坏情况
min = minimum(skipmissing(df.wages))
replace!(df.wages, missing => min)

# 与国家表连接
# 研究国家如何影响质量
leftjoin(company, countries, on=:location)
Julia 中的数据操作

熟悉数据

  • 花时间理解数据
    • 便于后续提取信息
    • 作图、打印结果等

了解你的数据

1 Photo by Myriam Jessier on Unsplash
Julia 中的数据操作

寻求帮助!

Google、Stack Overflow、DataCamp 标志

Julia 中的数据操作

玩得开心!

  • 享受过程,坚持下去,玩得开心!
Julia 中的数据操作

美国机场航班延误

航班数据结构

Julia 中的数据操作

Passons à la pratique !

Julia 中的数据操作

Preparing Video For Download...