文字列から数値を抽出する
UFOデータセットのlength_of_timeフィールドはテキスト型で、文字列の中に分単位の数値が含まれています。ここでは、正規表現を使ってそのテキストから数値を抽出します。
この演習はコースの一部です
Pythonで学ぶMachine Learningの前処理
演習の手順
- 適切な正規表現パターンを使って、
time_stringから数値を検索します。 .apply()メソッドを使って、length_of_time列の各行にreturn_minutes()を適用します。length_of_time列とminutes列の.head()をそれぞれ表示して、結果を比較します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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())