探索分组数据

Julia 中的数据操作

Katerina Zahradova

Instructor

为分组筛选

# 不同国家的公司
choc_comp_france = filter(chocolates -> chocolates.company_location == "France", chocolates)

choc_comp_brazil = filter(chocolates -> chocolates.company_location == "Brazil", chocolates)

choc_comp_peru = filter(chocolates -> chocolates.company_location == "Peru", chocolates)

choc_comp_belgium = filter(chocolates -> chocolates.company_location == "Belgium", chocolates)

...
Julia 中的数据操作

groupby() 来帮忙!

groupby 结构

Julia 中的数据操作

GroupedDataFrame

GroupedDataFrame 的结构

Julia 中的数据操作

groupby() 实战

# 按 company_location 分组
groupby(chocolates, :company_location)
GroupedDataFrame with 60 groups based on key: company_location
First Group (156 rows): company_location = "France"
Row  company   bean_origin  ...
     String    String       ...
_______________________________
1    A. Morin  Agua Grande  ...
...
Last Group(4 rows): company_location = "Ireland"
Row  company           bean_origin  ...
     String            String       ...
_______________________________
1    Wilkie's Organic  Amazonas     ...
Julia 中的数据操作

多列上的 groupby()

groupby(chocolates, [:company_location, :cocoa])
GroupedDataFrame with 373 groups based on keys: company_location, cocoa
First Group (5 rows): company_location = "France", cocoa = 63.0
Row  company   bean_origin  ...
     String    String       ...
_______________________________
1    A. Morin  Agua Grande  ...
...
Last Group (1 row): company_location = "Ireland", cocoa = 89.0
Row  company           bean_origin  ...
     String            String       ...
_______________________________
1    Wilkie's Organic  Amazonas     ...
Julia 中的数据操作

顺序很重要

按多列分组的示意图

Julia 中的数据操作

记录数

# 按国家分组
chocolates_by_country = groupby(chocolates, :company_location)  
# 每组记录数
combine(chocolates_by_country, nrow => :count) 
60x2 DataFrame
Row  company_location  nrow
     String31          Int64
______________________________
1    France            156
2    U.S.A.            764
3    Fiji              4
...
Julia 中的数据操作

按记录数排序

# 按国家分组
chocolates_by_country = groupby(chocolates, :company_location) 
# 按记录数排序
sort(combine(chocolates_by_country, nrow => :count), :count, rev = true)
60x2 DataFrame
Row  company_location  nrow
     String31          Int64
______________________________
1    U.S.A            764
2    France           156
3    Canada           125
...
Julia 中的数据操作

向量上的 unique()

# 唯一元素
unique(chocolates.company_location)
60-element Vector{String31}:
 "France"
 "U.S.A."
 "Fiji"
 "Ecuador"
 "Mexico"
 "Switzerland"
 "Netherlands"
...
Julia 中的数据操作

DataFrame 上的 unique()

# 仅保留唯一行的新数据框
unique(chocolates)
1795×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Julia 中的数据操作

指定列的 unique()

# 唯一的公司记录
unique(chocolates, :company)
416×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
# 唯一的公司与可可含量组合 
unique(chocolates, [:company, :cocoa])
968×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Julia 中的数据操作

Vamos praticar!

Julia 中的数据操作

Preparing Video For Download...