Khám phá dữ liệu theo nhóm

Xử lý dữ liệu trong Julia

Katerina Zahradova

Instructor

Lọc theo nhóm

# Công ty ở các quốc gia khác nhau
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)

...
Xử lý dữ liệu trong Julia

groupby() hỗ trợ bạn!

cấu trúc groupby

Xử lý dữ liệu trong Julia

GroupedDataFrames

Cấu trúc GroupedDataFrame

Xử lý dữ liệu trong Julia

groupby() thực tế

# Nhóm theo company_location
groupby(chocolates, :company_location)
GroupedDataFrame với 60 nhóm theo khóa: company_location
Nhóm đầu (156 hàng): company_location = "France"
Row  company   bean_origin  ...
     String    String       ...
_______________________________
1    A. Morin  Agua Grande  ...
...
Nhóm cuối (4 hàng): company_location = "Ireland"
Row  company           bean_origin  ...
     String            String       ...
_______________________________
1    Wilkie's Organic  Amazonas     ...
Xử lý dữ liệu trong Julia

groupby() trên nhiều cột

groupby(chocolates, [:company_location, :cocoa])
GroupedDataFrame với 373 nhóm theo khóa: company_location, cocoa
Nhóm đầu (5 hàng): company_location = "France", cocoa = 63.0
Row  company   bean_origin  ...
     String    String       ...
_______________________________
1    A. Morin  Agua Grande  ...
...
Nhóm cuối (1 hàng): company_location = "Ireland", cocoa = 89.0
Row  company           bean_origin  ...
     String            String       ...
_______________________________
1    Wilkie's Organic  Amazonas     ...
Xử lý dữ liệu trong Julia

Thứ tự quan trọng

groupby với nhiều cột

Xử lý dữ liệu trong Julia

Số bản ghi

# Nhóm theo quốc gia
chocolates_by_country = groupby(chocolates, :company_location)  
# Số bản ghi mỗi nhóm
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
...
Xử lý dữ liệu trong Julia

Sắp xếp theo số bản ghi

# Nhóm theo quốc gia
chocolates_by_country = groupby(chocolates, :company_location) 
# Sắp xếp theo số bản ghi
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
...
Xử lý dữ liệu trong Julia

unique() trên vector

# Phần tử duy nhất
unique(chocolates.company_location)
60-element Vector{String31}:
 "France"
 "U.S.A."
 "Fiji"
 "Ecuador"
 "Mexico"
 "Switzerland"
 "Netherlands"
...
Xử lý dữ liệu trong Julia

unique() trên DataFrame

# Tạo DataFrame chỉ gồm các hàng duy nhất
unique(chocolates)
1795×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Xử lý dữ liệu trong Julia

unique() với cột chỉ định

# Bản ghi công ty duy nhất
unique(chocolates, :company)
416×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
# Công ty VÀ hàm lượng ca cao duy nhất 
unique(chocolates, [:company, :cocoa])
968×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Xử lý dữ liệu trong Julia

Ayo berlatih!

Xử lý dữ liệu trong Julia

Preparing Video For Download...