본문 바로가기

Data Analysis/Kaggle

[Kaggle] Nexfilx movies and TV shows 데이터 분석 2 (데이터 전처리)

728x90

이전 내용은 아래 글에서 확인하실 수 있습니다.

[Data Analysis/Kaggle] - [Kaggle] Nexfilx movies and TV shows 데이터 분석 1 (데이터 확인 / 질문)

 

[Kaggle] Nexfilx movies and TV shows 데이터 분석 1 (데이터 확인 / 질문)

Nexfilx movies and TV shows 데이터셋 분석 분석 목적 : 넷플릭스 컨텐츠 분석으로 트렌드 파악 자료 출처 : https://www.kaggle.com/shivamb/netflix-shows Netflix Movies and TV Shows Listings of movi..

sks8410.tistory.com

3. 데이터 전처리

3-1 중복데이터 확인

netflix.duplicated().sum()

중복된 데이터는 없습니다.

3-2 사용하지 않는 컬럼 삭제

# `title`, `director`, `cast`, `description` 컬럼은 사용하지 않을 예정이므로 삭제

nf = netflix.drop(["title", "director", "cast", "description"], axis = 1)

print(nf.shape)
nf.head()

3-3 결측치 처리

# country 컬럼은 대체 할 수 있는 값을 알 수 없으므로 결측치를 Unknown 으로 변경

nf["country"] = nf["country"].fillna("Unknown")

print(nf.shape)
nf["country"].isnull().sum()

 

 

# 나머지 컬럼의 결측치는 비중이 매우 적으므로 삭제

nf = nf.dropna().reset_index(drop = True)

print(nf.shape)
nf.isnull().sum()

3-4 새로운 컬럼 생성 / 정리

3-4-1 country 컬럼

nf["country"].unique()

여러 국가가 들어있는 값이 있으므로, 제일 앞 국가명을 대표로 하여 new_country 컬럼에 등록하도록 하겠습니다.

 

 

nf["new_country"] = nf["country"].apply(lambda x: x.split(",")[0])

print(nf.shape)
nf.head()

3-4-2 date_added 컬럼

연도 / 월을 분리하여 각각 year_added / month_added 컬럼에 등록하도록 하겠습니다.

 

 

nf["year_added"] = nf["date_added"].apply(lambda x: x.split(",")[-1])
nf["month_added"] = nf["date_added"].apply(lambda x: x.split(" ")[0])

print(nf.shape)
nf.head()

 

 

nf["month_added"].unique()

 

nf[nf["month_added"] == ""]["date_added"].unique()

month_added 컬럼에서 ''" 로 된 값의 date_added 컬럼을 확인 해보면 월 앞에 공백이 있는 것을 알 수 있습니다.

따라서, 이 공백을 없애고 다시 date_added 컬럼의 월을 month_added에 등록 하도록 하겠습니다.

 

 

# 월 앞의 공백 제거
nf["date_added"] = nf["date_added"].str.strip()

nf[nf["month_added"] == ""]["date_added"].unique()

 

 

# date_added 의 월을 month_added 에 재 등록

nf["month_added"] = nf["date_added"].apply(lambda x: x.split(" ")[0])

print(nf.shape)
nf.head()

3-4-3 rating 컬럼

Amazon에서 제공하는 등급 정보(https://www.amazon.com/gp/help/customer/display.html?nodeId=G2C2CPZWGZWHZ42J)를 기준으로 구분된 등급을 rating_ages 컬럼에 등록 하도록 하겠습니다.

 

 

rating_ages = {
    'PG-13' : "Teens(13+)",
    'TV-MA' : "Adults(18+)",
    'PG' : "Older Kids(7+)",
    'TV-14' : "Young Adults(16+)",
    'TV-PG' : "Older Kids(7+)",
    'TV-Y' : "Kids(All)",
    'TV-Y7' : "Older Kids(7+)",
    'R' : "Adults(18+)",
    'TV-G' : "Kids(All)",
    'G' : "Kids(All)",
    'NC-17' : "Adults(18+)",
    'NR' : "Adults(18+)",
    'TV-Y7-FV' : "Older Kids(7+)",
    'UR' : "Adults(18+)"    
}

nf["rating_ages"] = nf["rating"].replace(rating_ages)

print(nf.shape)
print(nf["rating_ages"].unique())

nf.head()

3-4-4 listed_in 컬럼

한 작품에 여러가지 장르가 있으므로, 가장 앞에 있는 장르를 대표로 하여 genre 컬럼에 등록 하도록 하겠습니다.

 

 

nf["genre"] = nf["listed_in"].apply(lambda x: x.split(",")[0])

print(nf.shape)
nf.head()

3-5 컬럼 type 설정

# type 컬럼 - categorical type 으로 변경

nf["type"] = pd.Categorical(nf["type"])

# year_added 컬럼 - numeric type 으로 변경

nf["year_added"] = pd.to_numeric(nf["year_added"])

# rating_ages 컬럼 - categorical type 으로 변경

nf["rating_ages"] = pd.Categorical(nf["rating_ages"], categories = ['Teens(13+)', 'Adults(18+)', 'Older Kids(7+)', 'Young Adults(16+)',
                   'Kids(All)'])

nf.info()

 

728x90