시작하기무료로 시작하기

문자열에서 숫자 추출하기

UFO 데이터셋의 length_of_time 필드는 문자열 안에 분(minute) 수가 들어 있는 텍스트 필드입니다. 여기서는 정규 표현식을 사용해 그 텍스트에서 해당 숫자를 추출해 보겠습니다.

이 연습은 강의의 일부입니다

Python으로 배우는 Machine Learning 전처리

강의 보기

연습 안내

  • 적절한 정규식(RegEx) 패턴을 사용해 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())
코드 편집 및 실행