[Python pandas] 데이터프레임(dataframe)에서 데이터타입(dtype)에 부합하는 칼럼만 가져오기

728x90

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

# 라이브러리
import pandas as pd

# 데이터
mock_data = pd.read_csv('MOCK_DATA.csv')
mock_data.head(5)
  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
3 4 NaN Michele Freed NaN Male 95.30.236.76 Ferrol Spain Volvo NaN
4 5 821-40-7635 Skip Sallan ssallan4@nymag.com Male 200.83.222.220 Sheffield United Kingdom GMC Canyon
칼럼에서 object 타입만(include) 가져올 때
only_object = mock_data.select_dtypes(include='object')
only_object.head(3)
  ssn first_name last_name email gender ip_address city country car_make car_model
0 352-83-7834 Josselyn Commuzzo jcommuzzo0@shop-pro.jp Female NaN Aībak Afghanistan GMC Yukon XL 2500
1 129-55-2802 Aksel Cubbin acubbin1@rakuten.co.jp Male NaN Gaya Nigeria Cadillac CTS
2 421-78-0056 Marwin Vagg NaN Male NaN Huangjiakou China Chevrolet Tahoe
칼럼에서 int 를 제외한 타입만(exclude) 가져올 때
only_int = mock_data.select_dtypes(exclude=['int'])
only_int.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

 

728x90