Extracting numbers from strings
The length_of_time field in the UFO dataset is a text field that has the number of minutes within the string. Here, you'll extract that number from that text field using regular expressions.
Bu egzersiz
Preprocessing for Machine Learning in Python
kursunun bir parçasıdırEgzersiz talimatları
- Search
time_stringfor numbers using an appropriate RegEx pattern. - Use the
.apply()method to call thereturn_minutes()on every row of thelength_of_timecolumn. - Print out the
.head()of both thelength_of_timeandminutescolumns to compare.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
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())