Lidmaatschapsbeperkingen

Data opschonen in Python

Adel Nehme

Content Developer @DataCamp

 

 

 

 

 

 

 

Hoofdstuk 2 - Problemen met tekst- en categorische data

Data opschonen in Python

Categorieën en lidmaatschapsbeperkingen

Vooraf gedefinieerde eindige set categorieën

Type data Voorbeeldwaarden Numerieke weergave
Huwelijkse staat unmarried, married 0,1
Inkomensklasse huishouden 0-20K, 20-40K, ... 0,1, ..
Leningsstatus default,payed,no_loan 0,1,2

 

Huwelijkse staat kan alleen unmarried _of_ married zijn

Data opschonen in Python

Waarom kunnen deze problemen optreden?

categorical_issues

Data opschonen in Python

Hoe pak je deze problemen aan?

    categories

Data opschonen in Python

Een voorbeeld

# Lees studiedata en print
study_data = pd.read_csv('study.csv')
study_data
      name   birthday blood_type
1     Beth 2019-10-20         B-
2 Ignatius 2020-07-08         A-
3     Paul 2019-08-12         O+
4    Helen 2019-03-17         O-
5 Jennifer 2019-12-17         Z+
6  Kennedy 2020-04-27         A+
7    Keith 2019-04-19        AB+
# Juiste mogelijke bloedgroepen
categories
  blood_type
1         O-
2         O+
3         A-
4         A+
5         B+
6         B-
7        AB+
8        AB-
Data opschonen in Python

Een voorbeeld

# Lees studiedata en print
study_data = pd.read_csv('study.csv')
study_data
      name   birthday blood_type
1     Beth 2019-10-20         B-
2 Ignatius 2020-07-08         A-
3     Paul 2019-08-12         O+
4    Helen 2019-03-17         O-
5 Jennifer 2019-12-17         Z+  <--
6  Kennedy 2020-04-27         A+
7    Keith 2019-04-19        AB+
# Juiste mogelijke bloedgroepen
categories
  blood_type
1         O-
2         O+
3         A-
4         A+
5         B+
6         B-
7        AB+
8        AB-
Data opschonen in Python

Een opmerking over joins

Data opschonen in Python

Een left anti join op bloedgroepen

Data opschonen in Python

Een inner join op bloedgroepen

Data opschonen in Python

Inconsistente categorieën vinden

inconsistent_categories = set(study_data['blood_type']).difference(categories['blood_type'])
print(inconsistent_categories)
{'Z+'}
# Haal rijen met inconsistente categorieën op en print
inconsistent_rows = study_data['blood_type'].isin(inconsistent_categories)

study_data[inconsistent_rows]
      name   birthday blood_type
5 Jennifer 2019-12-17         Z+
Data opschonen in Python

Inconsistente categorieën verwijderen

inconsistent_categories = set(study_data['blood_type']).difference(categories['blood_type'])
inconsistent_rows = study_data['blood_type'].isin(inconsistent_categories)
inconsistent_data = study_data[inconsistent_rows]

# Verwijder inconsistente categorieën en houd alleen consistente data consistent_data = study_data[~inconsistent_rows]
      name   birthday blood_type
1     Beth 2019-10-20         B-
2 Ignatius 2020-07-08         A-
3     Paul 2019-08-12         O+
4    Helen 2019-03-17         O-
...    ...      ...          ...
Data opschonen in Python

Laten we oefenen!

Data opschonen in Python

Preparing Video For Download...