데이터 프레임: 두 판 사이의 차이
분류 추가, 코드 예제 오류·리드·코드블록 정리 |
데이터 프레임 예시를 노트북 스타일 셀로 분해하고 pandas 출력 예시를 실제 plain-text 결과 기준으로 교체 |
||
| 25번째 줄: | 25번째 줄: | ||
== Python 예시 == | == Python 예시 == | ||
< | <!-- pandas 출력은 노트북의 HTML 표와 다를 수 있으며 본 문서는 plain-text repr/stdout을 사용한다. --> | ||
학생 4명의 영어·수학 점수 데이터를 담은 데이터 프레임을 생성한다. | |||
<syntaxhighlight lang="python" line> | |||
import pandas as pd | |||
exam = pd.DataFrame({ | exam = pd.DataFrame({ | ||
'name': ['김지훈', '이유진', '박동현', '김민지'], | 'name': ['김지훈', '이유진', '박동현', '김민지'], | ||
| 35번째 줄: | 36번째 줄: | ||
'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> | ||
[[분류:프로그래밍과 데이터]] | [[분류:프로그래밍과 데이터]] | ||