Exploring grouped data

Data Manipulation in Julia

Katerina Zahradova

Instructor

Filtering for groups

# Companies in different countries
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)

...
Data Manipulation in Julia

groupby() to the rescue!

groupby structure

Data Manipulation in Julia

GroupedDataFrames

Structure of GroupedDataFrame

Data Manipulation in Julia

groupby() in action

# Group by 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     ...
Data Manipulation in Julia

groupby() on multiple columns

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     ...
Data Manipulation in Julia

Order matters

groupby with multiple columns

Data Manipulation in Julia

Number of records

# Group by country
chocolates_by_country = groupby(chocolates, :company_location)  
# Number of records per group
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
...
Data Manipulation in Julia

Sort by number of records

# Group by country
chocolates_by_country = groupby(chocolates, :company_location) 
# Sort the number of records
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
...
Data Manipulation in Julia

unique() on a vector

# Unique elements
unique(chocolates.company_location)
60-element Vector{String31}:
 "France"
 "U.S.A."
 "Fiji"
 "Ecuador"
 "Mexico"
 "Switzerland"
 "Netherlands"
...
Data Manipulation in Julia

unique() on a DataFrame

# Create a new DF containing only unique rows
unique(chocolates)
1795×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Data Manipulation in Julia

unique() with specified columns

# Unique company records
unique(chocolates, :company)
416×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
# Unique company AND cocoa contents 
unique(chocolates, [:company, :cocoa])
968×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Data Manipulation in Julia

Let's practice!

Data Manipulation in Julia

Preparing Video For Download...