Datenbereinigung in Python
Adel Nehme
VP of AI Curriculum, DataCamp

Geringste Anzahl Schritte, um von einer Zeichenfolge zur anderen zu gelangen

Geringste Anzahl Schritte, um von einer Zeichenfolge zur anderen zu gelangen


Bisher geringste Editierdistanz: 2

Levenshtein-Distanz (auch minimale Editierdistanz): 5

| Algorithmus | Wirkungsweise |
|---|---|
| Damerau-Levenshtein | Einfügen, Ersetzen, Löschen, Vertauschen |
| Levenshtein | Einfügen, Ersetzen, Löschen |
| Hamming | Ersetzen |
| Jaro-Distanz | Vertauschen |
| ... | ... |
Mögliche Pakete: nltk, thefuzz, textdistance ..
| Algorithmus | Wirkungsweise |
|---|---|
| Damerau-Levenshtein | Einfügen, Ersetzen, Löschen, Vertauschen |
| Levenshtein | Einfügen, Ersetzen, Löschen |
| Hamming | Ersetzen |
| Jaro-Distanz | Vertauschen |
| ... | ... |
Mögliche Pakete: thefuzz
# Lets us compare between two strings from thefuzz import fuzz# Compare reeding vs reading fuzz.WRatio('Reeding', 'Reading')
86
# Partial string comparison
fuzz.WRatio('Houston Rockets', 'Rockets')
90
# Partial string comparison with different order
fuzz.WRatio('Houston Rockets vs Los Angeles Lakers', 'Lakers vs Rockets')
86
# Import process
from thefuzz import process
# Define string and array of possible matches
string = "Houston Rockets vs Los Angeles Lakers"
choices = pd.Series(['Rockets vs Lakers', 'Lakers vs Rockets',
'Houson vs Los Angeles', 'Heat vs Bulls'])
process.extract(string, choices, limit = 2)
[('Rockets vs Lakers', 86, 0), ('Lakers vs Rockets', 86, 1)]
Kapitel 2
Anwendung von .replace(), um "eur" in "Europe" umzuwandeln.
Was ist, wenn es zu viele Varianten gibt?
"EU", "eur", "Europ", "Europa", "Erope", "Evropa"...
String-Ähnlichkeit!
print(survey['state'].unique())
id state
0 California
1 Cali
2 Calefornia
3 Calefornie
4 Californie
5 Calfornia
6 Calefernia
7 New York
8 New York City
...
categories
state
0 California
1 New York
# For each correct category for state in categories['state']:# Find potential matches in states with typoes matches = process.extract(state, survey['state'], limit = survey.shape[0])# For each potential match match for potential_match in matches: # If high similarity score if potential_match[1] >= 80:# Replace typo with correct category survey.loc[survey['state'] == potential_match[0], 'state'] = state

Datenbereinigung in Python