Q. 오늘도 데이터프레임이 주어졌다. 근데 특정 데이터 타입을 갖는 칼럼만 뽑아내고 싶다.. 어떻게 하면 될까...?
A) 👉🏻 'select_dtypes' method 사용!
1> seaborn 'titanic' dataset 불러오면
(seaborn.load_dataset docu 👉🏻 https://seaborn.pydata.org/generated/seaborn.load_dataset.html)
import pandas as pd
import seaborn as sns
titanic = sns.load_dataset('titanic')
titanic.head()
2> 먼저 column dtypes 확인!
#2) select columns by data type
titanic.dtypes
3> select_dtypes() method로 원하는 dtypes column만 출력!
(select_dtypes() docu 👉🏻 https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.select_dtypes.html)
→ include 또는 exclude 인자를 통해 원하는 dtypes를 포함할 수도 있고, 뺄 수도 있다...! (list 형식)
→ 그렇다면, include 또는 exclude 인자로 어떤 값이 들어갈 것인가? 원하는 dtypes 종류를 넣으면 된다
additional Q) 그렇다면 여기서 생기는 궁금증...! numpy.dtype형은 도대체 몇 개이고 object는 어떤 dtype들을 포함하는걸까..?
A) https://numpy.org/doc/stable/reference/arrays.scalars.html
ex) integer, inexact형 포함한 titanic data의 numeric type column만 출력하고 싶다면..?
titanic.select_dtypes(include='number').head()
ex) object형은 출력하고, numeric type data는 배제한 titanic data를 출력하고 싶다면..?
df_iris.select_dtypes(include='object', exclude='number').head()
<원하는 데이터 타입만 출력 완료>
* 출처 youtube) https://youtu.be/RlIiVeig3hc
'Python > Pandas&Numpy' 카테고리의 다른 글
pandas Tricks_04 👉🏻 'Build a DataFrame from multiple files (row-wise & column-wise) ' (Kevin by DataSchool) (0) | 2022.03.25 |
---|---|
pandas Tricks_03 👉🏻 'Convert Strings→numbers ' (Kevin by DataSchool) (0) | 2022.03.25 |
list comprehension (0) | 2022.03.23 |
pandas functions - cut, qcut (0) | 2022.03.23 |
pandas Tricks_01 👉🏻 'Reverse (row/column) Order' (Kevin by DataSchool) (0) | 2022.03.23 |
댓글