Gegevens per groep verkennen

Data manipulatie in Julia

Katerina Zahradova

Instructor

Filteren op groepen

# Bedrijven in verschillende landen
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 manipulatie in Julia

groupby() to the rescue!

groupby-structuur

Data manipulatie in Julia

GroupedDataFrames

Structuur van GroupedDataFrame

Data manipulatie in Julia

groupby() in actie

# Groeperen op company_location
groupby(chocolates, :company_location)
GroupedDataFrame met 60 groepen op sleutel: company_location
Eerste groep (156 rijen): company_location = "France"
Row  company   bean_origin  ...
     String    String       ...
_______________________________
1    A. Morin  Agua Grande  ...
...
Laatste groep (4 rijen): company_location = "Ireland"
Row  company           bean_origin  ...
     String            String       ...
_______________________________
1    Wilkie's Organic  Amazonas     ...
Data manipulatie in Julia

groupby() op meerdere kolommen

groupby(chocolates, [:company_location, :cocoa])
GroupedDataFrame met 373 groepen op sleutels: company_location, cocoa
Eerste groep (5 rijen): company_location = "France", cocoa = 63.0
Row  company   bean_origin  ...
     String    String       ...
_______________________________
1    A. Morin  Agua Grande  ...
...
Laatste groep (1 rij): company_location = "Ireland", cocoa = 89.0
Row  company           bean_origin  ...
     String            String       ...
_______________________________
1    Wilkie's Organic  Amazonas     ...
Data manipulatie in Julia

Volgorde is belangrijk

groupby met meerdere kolommen

Data manipulatie in Julia

Aantal rijen

# Groeperen op land
chocolates_by_country = groupby(chocolates, :company_location)  
# Aantal rijen per groep
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 manipulatie in Julia

Sorteren op aantal rijen

# Groeperen op land
chocolates_by_country = groupby(chocolates, :company_location) 
# Sorteer op aantal rijen
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 manipulatie in Julia

unique() op een vector

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

unique() op een DataFrame

# Maak een nieuwe DF met alleen unieke rijen
unique(chocolates)
1795×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Data manipulatie in Julia

unique() met opgegeven kolommen

# Unieke bedrijfsrecords
unique(chocolates, :company)
416×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
# Unieke combinaties van bedrijf EN cacaopercentage
unique(chocolates, [:company, :cocoa])
968×10 DataFrame
Row company  bean_origin  ...
    String   String       ...
_________________________________
1   A. Morin Agua Grande  ...
...
Data manipulatie in Julia

Laten we oefenen!

Data manipulatie in Julia

Preparing Video For Download...