[Python pandas] 데이터프레임(dataframe)에서 NA값 Drop 시키기

728x90
소개
# 데이터 프레임에서 전체 데이터 기준이나 특정 칼럼 기준으로 NA(결측치)를 제거합니다.
자료 출처
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html

라이브러리 및 데이터 불러오기

# 라이브러리
import pandas as pd

# 데이터
mock_data = pd.read_csv('MOCK_DATA.csv')
mock_data.head(3)
  id ssn first_name last_name email gender ip_address city country car_make car_model
0 1 352-83-7834 Josselyn Commuzzo jcommuzzo0@shop-pro.jp Female NaN Aībak Afghanistan GMC Yukon XL 2500
1 2 129-55-2802 Aksel Cubbin acubbin1@rakuten.co.jp Male NaN Gaya Nigeria Cadillac CTS
2 3 421-78-0056 Marwin Vagg NaN Male NaN Huangjiakou China Chevrolet Tahoe
데이터 프레임에서 NA가 1개라도 있는 ROW는 전부 제거
mock_drop_na = mock_data.dropna()
mock_drop_na.head()
  id ssn first_name last_name email gender ip_address city country car_make car_model
4 5 821-40-7635 Skip Sallan ssallan4@nymag.com Male 200.83.222.220 Sheffield United Kingdom GMC Canyon
5 6 854-88-6296 Belia Cushworth bcushworth5@blinklist.com Female 255.52.106.120 Nueve de Julio Argentina Dodge Ram
6 7 650-58-6287 Thane Baltzar tbaltzar6@infoseek.co.jp Male 89.251.85.176 Kikuchi Japan Pontiac Firefly
14 15 518-93-3198 Rafael Commander rcommandere@google.pl Male 67.252.156.166 Mandepa Barat Indonesia Buick Park Avenue
16 17 200-90-4726 Stacee Glass sglassg@cargocollective.com Female 78.236.229.147 Passa Quatro Brazil Mitsubishi Galant
데이터 프레임에서 특정 칼럼 기준 NA가 있을 경우 제거
mock_drop_email_na = mock_data.dropna(subset=['email'])
mock_drop_email_na.head()
  id ssn first_name last_name email gender ip_address city country car_make car_model
0 1 352-83-7834 Josselyn Commuzzo jcommuzzo0@shop-pro.jp Female NaN Aībak Afghanistan GMC Yukon XL 2500
1 2 129-55-2802 Aksel Cubbin acubbin1@rakuten.co.jp Male NaN Gaya Nigeria Cadillac CTS
4 5 821-40-7635 Skip Sallan ssallan4@nymag.com Male 200.83.222.220 Sheffield United Kingdom GMC Canyon
5 6 854-88-6296 Belia Cushworth bcushworth5@blinklist.com Female 255.52.106.120 Nueve de Julio Argentina Dodge Ram
6 7 650-58-6287 Thane Baltzar tbaltzar6@infoseek.co.jp Male 89.251.85.176 Kikuchi Japan Pontiac Firefly

 

728x90