데이터 프레임: 두 판 사이의 차이
분류 추가, 코드 예제 오류·리드·코드블록 정리 |
편집 요약 없음 |
||
| (같은 사용자의 중간 판 2개는 보이지 않습니다) | |||
| 1번째 줄: | 1번째 줄: | ||
'''데이터 프레임'''(Data Frame)은 행(Row)와 열(Column)로 이루어진 2차원 테이블 형태의 변수를 말한다. 데이터 분석에서 가장 흔하고 데이터의 특징을 잘 나타내는 핵심적인 형태이다. | '''데이터 프레임'''(Data Frame)은 행(Row)와 열(Column)로 이루어진 2차원 테이블 형태의 변수를 말한다. 데이터 분석에서 가장 흔하고 데이터의 특징을 잘 나타내는 핵심적인 형태이다. | ||
* 열(Column): 변수 또는 속성을 나타낸다. (예: 성별, 연령, 학점, 연봉 등) | * 열(Column): 변수 또는 속성을 나타낸다. (예: 성별, 연령, 학점, 연봉 등) | ||
* 행(Row): 관측치 또는 개별 사례를 나타낸다. (예: 남자, 21, 3.8 등) | * 행(Row): 관측치 또는 개별 사례를 나타낸다. (예: 남자, 21, 3.8 등) | ||
== 데이터 프레임의 특징 == | == 데이터 프레임의 특징 == | ||
* 리스트의 묶음: 여러 리스트(데이터의 묶음)를 하나로 묶어놓은 구조이다. | |||
* Key-Value 구조: 묶이는 것의 이름(열 이름)이 먼저 정의된다. | |||
* 대소문자 구분: 파이썬 문법 특성상 대소문자를 엄격하게 구분하므로 주의해야 한다. | |||
* 자동 인덱스 생성: 행을 식별하기 위해 0, 1, 2, 3... 형태의 인덱스가 자동으로 부여된다. | |||
* 외부 데이터 활용: 직접 만드는 것 외에도, 기존에 있는 파일(CSV, Excel 등) 데이터를 불러와서 사용할 수 있다. | |||
파이썬에서 '''데이터 프레임을 다루기 위해서는 `pandas` 라이브러리'''가 필요하다. | 파이썬에서 '''데이터 프레임을 다루기 위해서는 `pandas` 라이브러리'''가 필요하다. | ||
== Python 예시 == | |||
학생 4명의 영어·수학 점수 데이터를 담은 데이터 프레임을 생성한다. | |||
<syntaxhighlight lang="python" line> | |||
<syntaxhighlight lang="python"> | |||
import pandas as pd | import pandas as pd | ||
exam = pd.DataFrame({ | exam = pd.DataFrame({ | ||
'name': ['김지훈', '이유진', '박동현', '김민지'], | 'name': ['김지훈', '이유진', '박동현', '김민지'], | ||
| 35번째 줄: | 23번째 줄: | ||
'math': [50, 60, 100, 20] | 'math': [50, 60, 100, 20] | ||
}) | }) | ||
</syntaxhighlight> | |||
데이터 프레임 전체를 출력하면 다음과 같다. | |||
<syntaxhighlight lang="python" line start="7"> | |||
exam | 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() | 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() | 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() | 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'] | 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] | 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') | 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) | 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['total'] = exam['math'] + exam['english'] | ||
exam['mean'] = (exam['math'] + exam['english']) / 2 | |||
</syntaxhighlight> | |||
exam | 두 열이 추가된 최종 데이터 프레임을 확인한다. | ||
<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> | </syntaxhighlight> | ||
[[분류:프로그래밍과 데이터]] | [[분류:프로그래밍과 데이터]] | ||