Extraire des nombres depuis des chaînes
Le champ length_of_time dans le jeu de données UFO est un champ texte qui contient le nombre de minutes à l’intérieur de la chaîne. Ici, vous allez extraire ce nombre de ce champ texte à l’aide d’expressions régulières.
Cet exercice fait partie du cours
Prétraitement pour le Machine Learning en Python
Instructions
- Recherchez des nombres dans
time_stringà l’aide d’un motif RegEx approprié. - Utilisez la méthode
.apply()pour appelerreturn_minutes()sur chaque ligne de la colonnelength_of_time. - Affichez le
.head()des colonneslength_of_timeetminutespour comparer.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
def return_minutes(time_string):
# Search for numbers in time_string
num = re.____(____, ____)
if num is not None:
return int(num.group(0))
# Apply the extraction to the length_of_time column
ufo["minutes"] = ufo["length_of_time"].____
# Take a look at the head of both of the columns
print(ufo[[____]].head())