대문
임의의 문서로
로그인
설정
Coffee Wiki 소개
면책 조항
Coffee Wiki
검색
데이터 프레임 문서 원본 보기
←
데이터 프레임
문서 편집 권한이 없습니다. 다음 이유를 확인해주세요:
요청한 명령은 다음 권한을 가진 사용자에게 제한됩니다: writer.
문서의 원본을 보거나 복사할 수 있습니다.
'''데이터 프레임'''(Data Frame)은 행(Row)와 열(Column)로 이루어진 2차원 테이블 형태의 변수를 말한다. 데이터 분석에서 가장 흔하고 데이터의 특징을 잘 나타내는 핵심적인 형태이다. * 열(Column): 변수 또는 속성을 나타낸다. (예: 성별, 연령, 학점, 연봉 등) * 행(Row): 관측치 또는 개별 사례를 나타낸다. (예: 남자, 21, 3.8 등) == 데이터 프레임의 특징 == * 리스트의 묶음: 여러 리스트(데이터의 묶음)를 하나로 묶어놓은 구조이다. * Key-Value 구조: 묶이는 것의 이름(열 이름)이 먼저 정의된다. * 대소문자 구분: 파이썬 문법 특성상 대소문자를 엄격하게 구분하므로 주의해야 한다. * 자동 인덱스 생성: 행을 식별하기 위해 0, 1, 2, 3... 형태의 인덱스가 자동으로 부여된다. * 외부 데이터 활용: 직접 만드는 것 외에도, 기존에 있는 파일(CSV, Excel 등) 데이터를 불러와서 사용할 수 있다. 파이썬에서 '''데이터 프레임을 다루기 위해서는 `pandas` 라이브러리'''가 필요하다. == Python 예시 == 학생 4명의 영어·수학 점수 데이터를 담은 데이터 프레임을 생성한다. <syntaxhighlight lang="python" line> import pandas as pd exam = pd.DataFrame({ 'name': ['김지훈', '이유진', '박동현', '김민지'], 'english': [90, 80, 60, 70], 'math': [50, 60, 100, 20] }) </syntaxhighlight> 데이터 프레임 전체를 출력하면 다음과 같다. <syntaxhighlight lang="python" line start="7"> exam </syntaxhighlight> <syntaxhighlight lang="text"> name english math 0 김지훈 90 50 1 이유진 80 60 2 박동현 60 100 3 김민지 70 20 </syntaxhighlight> <code>head()</code>로 데이터의 앞부분을 확인한다. <syntaxhighlight lang="python" line start="8"> exam.head() </syntaxhighlight> <syntaxhighlight lang="text"> name english math 0 김지훈 90 50 1 이유진 80 60 2 박동현 60 100 3 김민지 70 20 </syntaxhighlight> <code>tail()</code>로 데이터의 뒷부분을 확인한다. <syntaxhighlight lang="python" line start="9"> exam.tail() </syntaxhighlight> <syntaxhighlight lang="text"> name english math 0 김지훈 90 50 1 이유진 80 60 2 박동현 60 100 3 김민지 70 20 </syntaxhighlight> <code>info()</code>는 행·열 개수, 데이터 타입, 결측치 등 요약 정보를 출력한다. <syntaxhighlight lang="python" line start="10"> exam.info() </syntaxhighlight> <syntaxhighlight lang="text"> <class 'pandas.core.frame.DataFrame'> RangeIndex: 4 entries, 0 to 3 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 name 4 non-null object 1 english 4 non-null int64 2 math 4 non-null int64 dtypes: int64(2), object(1) memory usage: 228.0+ bytes </syntaxhighlight> <code>describe()</code>는 수치형 열의 요약 통계량(평균, 표준편차, 최솟값, 최댓값 등)을 제공한다. <syntaxhighlight lang="python" line start="11"> exam.describe() </syntaxhighlight> <syntaxhighlight lang="text"> english math count 4.000000 4.000000 mean 75.000000 57.500000 std 12.909944 33.040379 min 60.000000 20.000000 25% 67.500000 42.500000 50% 75.000000 55.000000 75% 82.500000 70.000000 max 90.000000 100.000000 </syntaxhighlight> 대괄호 표기법으로 특정 열을 추출할 수 있다. <syntaxhighlight lang="python" line start="12"> exam['math'] </syntaxhighlight> <syntaxhighlight lang="text"> 0 50 1 60 2 100 3 20 Name: math, dtype: int64 </syntaxhighlight> 불리언 조건을 이용하여 특정 조건에 맞는 행만 추출할 수 있다. <syntaxhighlight lang="python" line start="13"> exam[exam['math'] >= 60] </syntaxhighlight> <syntaxhighlight lang="text"> name english math 1 이유진 80 60 2 박동현 60 100 </syntaxhighlight> <code>sort_values()</code>로 수학 점수를 기준으로 오름차순 정렬한다. <syntaxhighlight lang="python" line start="14"> exam.sort_values('math') </syntaxhighlight> <syntaxhighlight lang="text"> name english math 3 김민지 70 20 0 김지훈 90 50 1 이유진 80 60 2 박동현 60 100 </syntaxhighlight> <code>ascending=False</code>를 지정하면 내림차순(높은 점수 순)으로 정렬된다. <syntaxhighlight lang="python" line start="15"> exam.sort_values('math', ascending=False) </syntaxhighlight> <syntaxhighlight lang="text"> name english math 2 박동현 60 100 1 이유진 80 60 0 김지훈 90 50 3 김민지 70 20 </syntaxhighlight> 총점(<code>total</code>)과 평균(<code>mean</code>) 열을 새로 추가한다. 할당문은 반환값이 없으므로 출력이 나타나지 않는다. <syntaxhighlight lang="python" line start="16"> exam['total'] = exam['math'] + exam['english'] exam['mean'] = (exam['math'] + exam['english']) / 2 </syntaxhighlight> 두 열이 추가된 최종 데이터 프레임을 확인한다. <syntaxhighlight lang="python" line start="18"> exam </syntaxhighlight> <syntaxhighlight lang="text"> name english math total mean 0 김지훈 90 50 140 70.0 1 이유진 80 60 140 70.0 2 박동현 60 100 160 80.0 3 김민지 70 20 90 45.0 </syntaxhighlight> [[분류:프로그래밍과 데이터]]
데이터 프레임
문서로 돌아갑니다.