테이블형 데이터를 다룰 수 있는 다양한 기능을 가진 라이브러리
파이썬 데이터 분석을 위해 기본적으로 사용하는 라이브러리
pandas 라이브러리 import
보통 pandas는 pd로 줄여서 import
import pandas as pd
데이터를 다루기 위해서 Series(1차원), Dataframe(2차원)을 제공
엑셀을 예시로 생각하면 이해하기 편하다
Series는 1개의 인덱스와 1개의 열로 이루어져 있다.
Series 의 C . R . U . D
C (Create)
input
series data = pd.Series([70,60,90])
seriesdata
output
0 70
1 60
3 90
dtype : int64
input
series data = pd.Series([70,60,90]), index = [ '국어', '영어', '수학'])
seriesdata
output
국어 70
영어 60
수학 90
dtype : int64
R & U (Read & Update)
input
seriesdata.index
output
Index(['국어', '영어', '수학'], dtype = 'object')
input
seriesdata.index = ['미술', '음악', '체육']
seriesdata
output
미술 70
음악 60
체육 90
dytpe : int64
input
seriesdata.values # values = [] 형태로는 직접 수정이 안됨
output
array([70, 60, 90])
input
print (seriesdata ['미술'], seriesdata[0]
output
70 70
input
seriesdata['미술'] = 80
print (seriesdata['미술'], seriesdata[0])
output
80 80
D (Delete)
input
seriesdata
output
미술 80
음악 60
체육 90
dtype : int64
input
del seriesdata['미술']
seriesdata
output
음악 60
체육 90
dtype : int64
pandas 데이터 타입
pandas 데이터 타입은 파이썬과 다름
dtype으로 불리며, 주요 데이터 타입은 다음과 같다
object = python의 str(문자열)
int64 = python의 int(정수)
float = python의 float(부동소숫점)
bool = python의 bool(True of false)
이 외의 datetime64(날짜/시간)
가끔 data type 때문에 에러가 나는 경우가 있으므로, 데이터 타입에 대한 이해 및 데이터 타입 변경 기능을 알아두면 좋다.
input
seriesdata = pd.Series(['dave', 'alex', 'amir'])
series data
output
0 dave
1 alex
2 amir
dtype : object
input
seriesdata = pd.Series([1, 1, 1])
seriesdata
output
0 1
1 1
2 1
dtype: int64
데이터 타입 변경: Series.astype(변경할 타입)
input
seriesdata.astype('float')
output
0 1.0
1 1.0
2 1.0
dtype: float64
input
seriesdata = pd.Series(['dave', 1.1, 1.2])
seriesdata
output
0 dave
1 1.1
2 1.2
dtype: object
input
seriesdata = pd.Series([True, True, False])
seriesdata
output
0 True
1 True
2 False
dtype: bool
데이터프레임(Dataframe) 이해하기
데이터프레임은 테이블형 2차원 데이터이다.
엑셀/csv와 같이 row,column으로 구성되며, 인덱스도 두 개, row/column 각각 존재한다.
행의 레이블 : 인덱스 / 열의 레이블 : 컬럼
import pandas ps pd # 라이브러리 임포트
C(Create)
input
df = pd.DataFrame({
"미국": [2.1, 2.2, 2.3],
"한국": [0.4, 0.5, 0.45],
"중국": [10, 13, 15]
})
df
output
미국 한국 중국
0 2.1 0.4 10
1 2.2 0.50 13
2 2.3 0.45 15
C(Create)(인덱스와 함께 생성)
input
df = pd.DataFrame({
"미국": [2.1, 2.2, 2.3],
"한국": [0.4, 0.5, 0.45],
"중국": [10, 13, 15],
index=[2000, 2010, 2020]
)
df
output
미국 한국 중국
2000 2.1 0.4 10
2010 2.2 0.5 13
2020 2.3 0.45 15
R&U (Read & Update)
Series는 index와 values
index
df.index
output
Int64Index([2000, 2010, 2020], dtype='int64')
input
df.index = [2001, 2002, 2003]
df
output
미국 한국 중국
2001 2.1 0.40 10
2002 2.2 0.50 13
2003 2.3 0.45 15
input
df.columns
output
Index(['미국', '한국', '중국'], dtype='object')
input
df.columns = ['일본', '필리핀', '러시아']
df.values
output
array([[2.1, 0.4, 10. ],
[2.2, 0.5, 13. ],
[2.3, 0.45, 15. ]])
인덱스로 특정 컬럼 선택하기
input
df = pd.DataFrame({
"년도": [2000, 2010, 2020],
"미국": [2.1, 2.2, 2.3],
"한국": [0.4, 0.5, 0.45],
"중국": [10, 13, 15]
})
df
output
년도 미국 한국 중국
0 2000 2.1 0.40 10
1 2010 2.2 0.50 13
2 2020 2.3 0.45 15
input
df = df.set_index('년도')
df
output
미국 한국 중국
년도
2000 2.1 0.40 10
2010 2.2 0.50 13
2020 2.3 0.45 15