Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 판다스
- 구름톤
- Compiler
- ML
- 프로그래머스
- streamlit
- xla
- hackerrank
- 컴파일러
- 프로그래밍
- Relation Extraction
- 코딩테스트
- 인터프리터언어
- python
- TF-IDF
- 오라클
- NumPy
- 해시
- 해시테이블
- 코딩
- string 모듈
- 파이썬
- sql
- 컴파일언어
- 해커랭크
- Oracle
- 자료구조
- pandas
- 코테
- BM25
Archives
- Today
- Total
Mo!
(Python) 데이터프레임에서 특정 열을 제외하고 선택하기 본문
1. seaborn의 내장 데이터셋 목록 확인하기
seaborn.get_dataset_names() : seaborn의 내장된 데이터 셋 목록을 확인하기
import seaborn as sns
sns.get_dataset_names() #seaborn의 내장된 데이터 셋 목록을 확인하는 방법
['anagrams',
'anscombe',
'attention',
'brain_networks',
'car_crashes',
'diamonds',
'dots',
'exercise',
'flights',
'fmri',
'gammas',
'geyser',
'iris',
'mpg',
'penguins',
'planets',
'taxis',
'tips',
'titanic']
2. titanic 데이터셋을 불러와서 titanic이라는 변수에 담기
seaborn.load_dataset('데이터셋 명') : seaborn에 내장된 데이터셋 불러오기
titanic = sns.load_dataset('titanic')
titanic.head() # 상위 5개 행 출력
3. titanic 데이터 프레임에서 특정 열을 제외하고 가져오기
parch, deck 컬럼을 제외하고 가져오는 방법은?
방법 1) dataframe.columns != '컬럼명'
titanic.loc[:, (titanic.columns != 'parch') & (titanic.columns != 'deck')]
방법 2) ~dataframe.columns.isin( ['컬럼명', '컬럼명'] )
~titanic.columns.isin(['parch', 'deck'])
array([ True, True, True, True, True, False, True, True, True,
True, True, False, True, True, True])
위의 코드를 실행하면 parch 컬럼과 deck 컬럼에 해당하는 부분은 FALSE로 반환되고 있다.
위의 코드를 loc의 조건으로 넣어주면 데이터프레임 형태로 반환된다.
titanic.loc[:, ~titanic.columns.isin(['parch', 'deck']) ]
방법 3) one-line for문 사용하기
[i for i in list(dataframe.columns) if i not in ['제외할 컬럼명1', '제외할 컬럼명2']]
[i for i in list(titanic.columns) if i not in ['parch', 'deck']]
['survived',
'pclass',
'sex',
'age',
'sibsp',
'fare',
'embarked',
'class',
'who',
'adult_male',
'embark_town',
'alive',
'alone']
위의 코드를 실행하면 parch 컬럼과 deck 컬럼을 제외한 titanic의 컬럼 목록이 반환된다.
titanic.loc[:, [i for i in list(titanic.columns) if i not in ['parch', 'deck']] ]
# titanic[ [i for i in list(titanic.columns) if i not in ['parch', 'deck']] ]
위의 코드를 loc의 조건이나 데이터프레임 열 선택 조건으로 주면 된다.
'Python' 카테고리의 다른 글
(Python) 데이터프레임 살펴보기 (0) | 2022.02.24 |
---|---|
(Python) np.arange(), np.zeros(), np.ones() (0) | 2022.02.24 |
(Python) while문 개념 (3) - 중첩 while문 [구구단 출력하기] (0) | 2021.12.13 |
(Python) while문 개념(2) - continue, break (0) | 2021.12.13 |
(Python) while문 개념(1) (0) | 2021.12.13 |
Comments