실습 1주차 :
딕셔너리 시리즈 변환
# -*- coding: utf-8 -*-
# pandas 불러오기
import pandas as pd
# k:v 구조를 갖는 딕셔너리를 만들고, 변수 dict_data에 저장
dict_data = {'a': 1, 'b': 2, 'c': 3}
# 판다스 Series() 함수로 딕셔너리(dict_data)를 시리즈로 변환. 변수 sr에 저장
sr = pd.Series(dict_data)
# 변수 sr의 자료형 출력
print(type(sr))
print('\n')
# 변수 sr에 저장되어 있는 시리즈 객체를 출력
print(sr)
<class 'pandas.core.series.Series'>
a 1
b 2
c 3
dtype: int64
import pandas as pd
# 리스트를 시리즈로 변환하여 변수 sr에 저장
list_data = ['2019-01-02', 3.14, 'ABC', 100, True]
sr = pd.Series(list_data)
print(sr)
0 2019-01-02
1 3.14
2 ABC
3 100
4 True
dtype: object
# 인덱스 배열은 변수 idx에 저장. 데이터 값 배열은 변수 val에 저장
idx = sr.index
val = sr.values
print(idx)
print('\n')
print(val)
RangeIndex(start=0, stop=5, step=1)
['2019-01-02' 3.14 'ABC' 100 True]
원소선택
# -*- coding: utf-8 -*-
import pandas as pd
# 투플을 시리즈로 변환(index 옵션에 인덱스 이름을 지정)
tup_data = ('영인', '2010-05-01', '여', True)
sr = pd.Series(tup_data, index=['이름', '생년월일', '성별', '학생여부'])
이름 영인
생년월일 2010-05-01
성별 여
학생여부 True
dtype: object
# 원소를 1개 선택
print(sr[0]) # sr의 1 번째 원소를 선택 (정수형 위치 인덱스를 활용)
print(sr['이름']) # '이름' 라벨을 가진 원소를 선택 (인덱스 이름을 활용)
영인
영인
# 여러 개의 원소를 선택 (인덱스 범위 지정)
print(sr[1 : 2])
print('\n')
print(sr['생년월일' : '성별'])
생년월일 2010-05-01
dtype: object
생년월일 2010-05-01
성별 여
dtype: object
딕셔너리 →데이터프레임 변환
# -*- coding: utf-8 -*-
import pandas as pd
# 열이름을 key로 하고, 리스트를 value로 갖는 딕셔너리 정의(2차원 배열)
dict_data = {'c0':[1,2,3], 'c1':[4,5,6], 'c2':[7,8,9], 'c3':[10,11,12], 'c4':[13,14,15]}
# 판다스 DataFrame() 함수로 딕셔너리를 데이터프레임으로 변환. 변수 df에 저장.
df = pd.DataFrame(dict_data)
# df의 자료형 출력
print(type(df))
print('\n')
# 변수 df에 저장되어 있는 데이터프레임 객체를 출력
print(df)
<class 'pandas.core.frame.DataFrame'>
c0 c1 c2 c3 c4
0 1 4 7 10 13
1 2 5 8 11 14
2 3 6 9 12 15
# -*- coding: utf-8 -*-
import pandas as pd
# 행 인덱스/열 이름 지정하여, 데이터프레임 만들기
df = pd.DataFrame([[15, '남', '덕영중'], [17, '여', '수리중']],
index=['준서', '예은'],
columns=['나이', '성별', '학교'])
# 행 인덱스, 열 이름 확인하기
print(df) #데이터프레임
print('\n')
print(df.index) #행 인덱스
print('\n')
print(df.columns) #열 이름
나이 성별 학교
준서 15 남 덕영중
예은 17 여 수리중
Index(['준서', '예은'], dtype='object')
Index(['나이', '성별', '학교'], dtype='object')
# 행 인덱스, 열 이름 변경하기
df.index=['학생1', '학생2']
df.columns=['연령', '남녀', '소속']
print(df) #데이터프레임
print('\n')
print(df.index) #행 인덱스
print('\n')
print(df.columns) #열 이름
연령 남녀 소속
학생1 15 남 덕영중
학생2 17 여 수리중
Index(['학생1', '학생2'], dtype='object')
Index(['연령', '남녀', '소속'], dtype='object')
# -*- coding: utf-8 -*-
import pandas as pd
# 행 인덱스/열 이름 지정하여, 데이터프레임 만들기
df = pd.DataFrame([[15, '남', '덕영중'], [17, '여', '수리중']],
index=['준서', '예은'],
columns=['나이', '성별', '학교'])
# 데이터프레임 df 출력
print(df)
print("\n")
# 열 이름 중, '나이'를 '연령'으로, '성별'을 '남녀'로, '학교'를 '소속'으로 바꾸기
df.rename(columns={'나이':'연령', '성별':'남녀', '학교':'소속'}, inplace=True)
# df의 행 인덱스 중에서, '준서'를 '학생1'로, '예은'을 '학생2'로 바꾸기
df.rename(index={'준서':'학생1', '예은':'학생2' }, inplace=True)
# df 출력(변경 후)
print(df)
나이 성별 학교
준서 15 남 덕영중
예은 17 여 수리중
연령 남녀 소속
학생1 15 남 덕영중
학생2 17 여 수리중
삭제 axis=0 column 삭제, axis=1 index에서 삭제
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'수학' : [ 90, 80, 70], '영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100], '체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data, index=['서준', '우현', '인아'])
print(df)
print('\n')
# 데이터프레임 df를 복제하여 변수 df4에 저장. df4의 1개 열(column)을 삭제
df4 = df.copy()
df4.drop('수학', axis=1, inplace=True)
print(df4)
print('\n')
# 데이터프레임 df를 복제하여 변수 df5에 저장. df5의 2개 열(column)을 삭제
df5 = df.copy()
df5.drop(['영어', '음악'], axis=1, inplace=True)
print(df5)
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'수학' : [ 90, 80, 70], '영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100], '체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data, index=['서준', '우현', '인아'])
print(df)
print('\n')
# 데이터프레임 df를 복제하여 변수 df2에 저장. df2의 1개 행(row)을 삭제
df2 = df.copy()
df2.drop('우현', inplace=True)
print(df2)
# 데이터프레임 df를 복제하여 변수 df3에 저장. df3의 2개 행(row)을 삭제
df3 = df.copy()
df3.drop(['우현', '인아'], axis=0, inplace=True)
print(df3)
print('\n')
선택 iloc, loc
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'수학' : [ 90, 80, 70], '영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100], '체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data, index=['서준', '우현', '인아'])
print(df) # 데이터프레임 출력
print('\n')
# 행 인덱스를 사용하여 행 1개를 선택
label1 = df.loc['서준'] # loc 인덱서 활용 함수아니고 특이하게 [] 씀
position1 = df.iloc[0] # iloc 인덱서 활용 함수아니고 특이하게 [] 씀
print(label1)
print('\n')
print(position1)
수학 영어 음악 체육
서준 90 98 85 100
우현 80 89 95 90
인아 70 95 100 90
수학 90
영어 98
음악 85
체육 100
Name: 서준, dtype: int64
수학 90
영어 98
음악 85
체육 100
Name: 서준, dtype: int64
# 행 인덱스를 사용하여 2개 이상의 행 선택
label2 = df.loc[['서준', '우현']]
position2 = df.iloc[[0, 1]]
print(label2)
print('\n')
print(position2)
수학 영어 음악 체육
서준 90 98 85 100
우현 80 89 95 90
수학 영어 음악 체육
서준 90 98 85 100
우현 80 89 95 90
# 행 인덱스의 범위를 지정하여 행 선택
label3 = df.loc['서준':'우현']
position3 = df.iloc[0:1]
print(label3)
print('\n')
print(position3)
수학 영어 음악 체육
서준 90 98 85 100
우현 80 89 95 90
수학 영어 음악 체육
서준 90 98 85 100
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
print(df)
print(type(df))
print('\n')
# '수학' 점수 데이터만 선택. 변수 math1에 저장
math1 = df['수학']
print(math1)
print(type(math1))
print('\n')
# '영어' 점수 데이터만 선택. 변수 english에 저장
english = df.영어
print(english)
print(type(english))
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
<class 'pandas.core.frame.DataFrame'>
0 90
1 80
2 70
Name: 수학, dtype: int64
<class 'pandas.core.series.Series'>
0 98
1 89
2 95
Name: 영어, dtype: int64
<class 'pandas.core.series.Series'>
# '음악', '체육' 점수 데이터를 선택. 변수 music_gym 에 저장
music_gym = df[['음악', '체육']]
print(music_gym)
print(type(music_gym))
print('\n')
# '수학' 점수 데이터만 선택. 변수 math2에 저장
math2 = df[['수학']]
print(math2)
print(type(math2))
음악 체육
0 85 100
1 95 90
2 100 90
<class 'pandas.core.frame.DataFrame'>
수학
0 90
1 80
2 70
<class 'pandas.core.frame.DataFrame'>
- 주의점 [[]] 사용 ⇒ dataframe
- [] 사용 ⇒ serires
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
# '이름' 열을 새로운 인덱스로 지정하고, df 객체에 변경사항 반영
df.set_index('이름', inplace=True)
print(df)
# '음악', '체육' 점수 데이터를 선택. 변수 music_gym 에 저장
music_gym = df[['음악', '체육']]
print(music_gym)
print(type(music_gym))
print('\n')
# '수학' 점수 데이터만 선택. 변수 math2에 저장
math2 = df[['수학']]
print(math2)
print(type(math2))
음악 체육
0 85 100
1 95 90
2 100 90
<class 'pandas.core.frame.DataFrame'>
수학
0 90
1 80
2 70
<class 'pandas.core.frame.DataFrame'>
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
# '이름' 열을 새로운 인덱스로 지정하고, df 객체에 변경사항 반영
df.**set_index**('이름', inplace=True)
print(df)
# 데이터프레임 df의 특정 원소 1개 선택 ('서준'의 '음악' 점수)
a = df.loc**['서준', '음악']**
print(a)
b = df.iloc[0, 2]
print(b)
85
85
# 데이터프레임 df의 특정 원소 2개 이상 선택 ('서준'의 '음악', '체육' 점수)
c = df.**loc['서준', ['음악', '체육']]**
print(c)
d = df.**iloc[0, [2, 3]]**
print(d)
e = df.**loc['서준', '음악':'체육']**
print(e)
f = df.**iloc[0, 2:]**
print(f)
# df의 2개 이상의 행과 열로부터 원소 선택 ('서준', '우현'의 '음악', '체육' 점수)
g = df.**loc[['서준', '우현'], ['음악', '체육']]**
print(g)
h = df.**iloc[[0, 1], [2, 3]]**
print(h)
i = df.**loc['서준':'우현', '음악':'체육']**
print(i)
j = df**.iloc[0:2, 2:]**
print(j)
추가
열
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
print(df)
print('\n')
# 데이터프레임 df에 '국어' 점수 열(column)을 추가. 데이터 값은 80 지정
df['국어'] = 80
print(df)
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
이름 수학 영어 음악 체육 국어
0 서준 90 98 85 100 80
1 우현 80 89 95 90 80
2 인아 70 95 100 90 80
행
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : ['서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
print(df)
print('\n')
# 새로운 행(row)을 추가 - 같은 원소 값을 입력
df.loc[3] = 0
print(df)
print('\n')
# 새로운 행(row)을 추가 - 원소 값 여러 개의 배열 입력
df.loc[4] = ['동규', 90, 80, 70, 60]
print(df)
print('\n')
# 새로운 행(row)을 추가 - 기존 행을 복사
df.loc['행5'] = df.loc[3]
print(df)
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
3 0 0 0 0 0
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
3 0 0 0 0 0
4 동규 90 80 70 60
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
3 0 0 0 0 0
4 동규 90 80 70 60
행5 0 0 0 0 0
값 변경
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
# '이름' 열을 새로운 인덱스로 지정하고, df 객체에 변경사항 반영
df.set_index('이름', inplace=True)
print(df)
print('\n')
# 데이터프레임 df의 특정 원소를 변경하는 방법: '서준'의 '체육' 점수
df.iloc[0][3] = 80
print(df)
print('\n')
df.loc['서준']['체육'] = 90
print(df)
print('\n')
df.loc['서준', '체육'] = 100
print(df)
수학 영어 음악 체육
이름
서준 90 98 85 100
우현 80 89 95 90
인아 70 95 100 90
수학 영어 음악 체육
이름
서준 90 98 85 80
우현 80 89 95 90
인아 70 95 100 90
수학 영어 음악 체육
이름
서준 90 98 85 90
우현 80 89 95 90
인아 70 95 100 90
수학 영어 음악 체육
이름
서준 90 98 85 100
우현 80 89 95 90
인아 70 95 100 90
# 데이터프레임 df의 원소 여러 개를 변경하는 방법: '서준'의 '음악', '체육' 점수
df.loc['서준', ['음악', '체육']] = 50
print(df)
print('\n')
df.loc['서준', ['음악', '체육']] = 100, 50
print(df)
수학 영어 음악 체육
이름
서준 90 98 50 50
우현 80 89 95 90
인아 70 95 100 90
수학 영어 음악 체육
이름
서준 90 98 100 50
우현 80 89 95 90
인아 70 95 100 90
행, 열 바꾸기
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
print(df)
print('\n')
# 데이터프레임 df를 전치하기 (메소드 활용)
df = df**.transpose()**
print(df)
print('\n')
# 데이터프레임 df를 다시 전치하기 (클래스 속성 활용)
df = df.**T**
print(df)
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
0 1 2
이름 서준 우현 인아
수학 90 80 70
영어 98 89 95
음악 85 95 100
체육 100 90 90
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
특정열 행인덱스 설정
# -*- coding: utf-8 -*-
import pandas as pd
# DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
exam_data = {'이름' : [ '서준', '우현', '인아'],
'수학' : [ 90, 80, 70],
'영어' : [ 98, 89, 95],
'음악' : [ 85, 95, 100],
'체육' : [ 100, 90, 90]}
df = pd.DataFrame(exam_data)
print(df)
print('\n')
# 특정 열(column)을 데이터프레임의 행 인덱스(index)로 설정
ndf = df.set_index(['이름'])
print(ndf)
print('\n')
ndf2 = ndf.set_index('음악')
print(ndf2)
print('\n')
ndf3 = ndf.**set_index(['수학', '음악'])**
print(ndf3)
이름 수학 영어 음악 체육
0 서준 90 98 85 100
1 우현 80 89 95 90
2 인아 70 95 100 90
수학 영어 음악 체육
이름
서준 90 98 85 100
우현 80 89 95 90
인아 70 95 100 90
수학 영어 체육
음악
85 90 98 100
95 80 89 90
100 70 95 90
영어 체육
수학 음악
90 85 98 100
80 95 89 90
70 100 95 90
# -*- coding: utf-8 -*-
import pandas as pd
# 딕셔서리를 정의
dict_data = {'c0':[1,2,3], 'c1':[4,5,6], 'c2':[7,8,9], 'c3':[10,11,12], 'c4':[13,14,15]}
# 딕셔서리를 데이터프레임으로 변환. 인덱스를 [r0, r1, r2]로 지정
df = pd.DataFrame(dict_data, index=['r0', 'r1', 'r2'])
print(df)
print('\n')
# 인덱스를 [r0, r1, r2, r3, r4]로 재지정
new_index = ['r0', 'r1', 'r2', 'r3', 'r4']
ndf = df.reindex(new_index)
print(ndf)
print('\n')
# reindex로 발생한 NaN값을 숫자 0으로 채우기
new_index = ['r0', 'r1', 'r2', 'r3', 'r4']
ndf2 = df.**reindex(new_index, fill_value=0)**
print(ndf2)
c0 c1 c2 c3 c4
r0 1 4 7 10 13
r1 2 5 8 11 14
r2 3 6 9 12 15
c0 c1 c2 c3 c4
r0 1.0 4.0 7.0 10.0 13.0
r1 2.0 5.0 8.0 11.0 14.0
r2 3.0 6.0 9.0 12.0 15.0
r3 NaN NaN NaN NaN NaN
r4 NaN NaN NaN NaN NaN
c0 c1 c2 c3 c4
r0 1 4 7 10 13
r1 2 5 8 11 14
r2 3 6 9 12 15
r3 0 0 0 0 0
r4 0 0 0 0 0
# -*- coding: utf-8 -*-
import pandas as pd
# 딕셔서리를 정의
dict_data = {'c0':[1,2,3], 'c1':[4,5,6], 'c2':[7,8,9], 'c3':[10,11,12], 'c4':[13,14,15]}
# 딕셔서리를 데이터프레임으로 변환. 인덱스를 [r0, r1, r2]로 지정
df = pd.DataFrame(dict_data, index=['r0', 'r1', 'r2'])
print(df)
print('\n')
# 행 인덱스를 정수형으로 초기화
ndf = df.**reset_index()**
print(ndf)
c0 c1 c2 c3 c4
r0 1 4 7 10 13
r1 2 5 8 11 14
r2 3 6 9 12 15
index c0 c1 c2 c3 c4
0 r0 1 4 7 10 13
1 r1 2 5 8 11 14
2 r2 3 6 9 12 15
정렬
# -*- coding: utf-8 -*-
import pandas as pd
# 딕셔서리를 정의
dict_data = {'c0':[1,2,3], 'c1':[4,5,6], 'c2':[7,8,9], 'c3':[10,11,12], 'c4':[13,14,15]}
# 딕셔서리를 데이터프레임으로 변환. 인덱스를 [r0, r1, r2]로 지정
df = pd.DataFrame(dict_data, index=['r0', 'r1', 'r2'])
print(df)
print('\n')
# 내림차순으로 행 인덱스 정렬
ndf = df.sort_index(ascending=False)
print(ndf)
c0 c1 c2 c3 c4
r0 1 4 7 10 13
r1 2 5 8 11 14
r2 3 6 9 12 15
c0 c1 c2 c3 c4
r2 3 6 9 12 15
r1 2 5 8 11 14
r0 1 4 7 10 13
나누기
# -*- coding: utf-8 -*-
# 라이브러리 불러오기
import pandas as pd
# 딕셔너리 데이터로 판다스 시리즈 만들기
student1 = pd.Series({'국어':100, '영어':80, '수학':90})
print(student1)
print('\n')
# 학생의 과목별 점수를 200으로 나누기
percentage = **student1 / 200**
print(percentage)
print('\n')
print(type(percentage))
국어 100
영어 80
수학 90
dtype: int64
국어 0.50
영어 0.40
수학 0.45
dtype: float64
<class 'pandas.core.series.Series'>
사칙연산
# -*- coding: utf-8 -*-
# 라이브러리 불러오기
import pandas as pd
# 딕셔너리 데이터로 판다스 시리즈 만들기
student1 = pd.Series({'국어':100, '영어':80, '수학':90})
student2 = pd.Series({'수학':80, '국어':90, '영어':80})
print(student1)
print('\n')
print(student2)
print('\n')
# 두 학생의 과목별 점수로 사칙연산 수행
addition = student1 + student2 #덧셈
subtraction = student1 - student2 #뺄셈
multiplication = student1 * student2 #곱셈
division = student1 / student2 #나눗셈
print(type(division))
print('\n')
# 사칙연산 결과를 데이터프레임으로 합치기 (시리즈 -> 데이터프레임)
result = pd.DataFrame([addition, subtraction, multiplication, division],
index=['덧셈', '뺄셈', '곱셈', '나눗셈'])
print(result)
국어 100
영어 80
수학 90
dtype: int64
수학 80
국어 90
영어 80
dtype: int64
<class 'pandas.core.series.Series'>
국어 수학 영어
덧셈 190.000000 170.000 160.0
뺄셈 10.000000 10.000 0.0
곱셈 9000.000000 7200.000 6400.0
나눗셈 1.111111 1.125 1.0
하지만 NaN 값이 있다면?
# -*- coding: utf-8 -*-
# 라이브러리 불러오기
import pandas as pd
import numpy as np
# 딕셔너리 데이터로 판다스 시리즈 만들기
student1 = pd.Series({'국어':np.nan, '영어':80, '수학':90})
student2 = pd.Series({'수학':80, '국어':90})
print(student1)
print('\n')
print(student2)
print('\n')
# 두 학생의 과목별 점수로 사칙연산 수행 (시리즈 vs. 시리즈)
addition = student1 + student2 #덧셈
subtraction = student1 - student2 #뺄셈
multiplication = student1 * student2 #곱셈
division = student1 / student2 #나눗셈
print(type(division))
print('\n')
# 사칙연산 결과를 데이터프레임으로 합치기 (시리즈 -> 데이터프레임)
result = pd.DataFrame([addition, subtraction, multiplication, division],
index=['덧셈', '뺄셈', '곱셈', '나눗셈'])
print(result)
국어 NaN
영어 80.0
수학 90.0
dtype: float64
수학 80
국어 90
dtype: int64
<class 'pandas.core.series.Series'>
국어 수학 영어
덧셈 NaN 170.000 NaN
뺄셈 NaN 10.000 NaN
곱셈 NaN 7200.000 NaN
나눗셈 NaN 1.125 NaN
df+df
# -*- coding: utf-8 -*-
# 라이브러리 불러오기
import pandas as pd
import seaborn as sns
# titanic 데이터셋에서 age, fare 2개 열을 선택하여 데이터프레임 만들기
titanic = sns.load_dataset('titanic')
df = titanic.loc[:, ['age','fare']]
print(df.tail()) #마지막 5행을 표시
print('\n')
print(type(df))
print('\n')
# 데이터프레임에 숫자 10 더하기
addition = df + 10
print(addition.tail()) #마지막 5행을 표시
print('\n')
print(type(addition))
print('\n')
# 데이터프레임끼리 연산하기 (additon - df)
subtraction = addition - df
print(subtraction.tail()) #마지막 5행을 표시
print('\n')
print(type(subtraction))
age fare
886 27.0 13.00
887 19.0 30.00
888 NaN 23.45
889 26.0 30.00
890 32.0 7.75
<class 'pandas.core.frame.DataFrame'>
age fare
886 37.0 23.00
887 29.0 40.00
888 NaN 33.45
889 36.0 40.00
890 42.0 17.75
<class 'pandas.core.frame.DataFrame'>
age fare
886 10.0 10.0
887 10.0 10.0
...
890 10.0 10.0
<class 'pandas.core.frame.DataFrame'>
csv 파일
# -*- coding: utf-8 -*-
# 라이브러리 불러오기
import pandas as pd
# 파일경로를 찾고, 변수 file_path에 저장
file_path = './read_csv_sample.csv'
# read_csv() 함수로 데이터프레임 변환. 변수 df1에 저장
df1 = pd.read_csv(file_path)
print(df1)
print('\n')
# read_csv() 함수로 데이터프레임 변환. 변수 df2에 저장. header=None 옵션
df2 = pd.read_csv(file_path, header=None)
print(df2)
print('\n')
# read_csv() 함수로 데이터프레임 변환. 변수 df3에 저장. index_col=None 옵션
df3 = pd.read_csv(file_path, index_col=None)
print(df3)
print('\n')
# read_csv() 함수로 데이터프레임 변환. 변수 df4에 저장. index_col='c0' 옵션
df4 = pd.read_csv(file_path, index_col='c0')
print(df4)
c0 c1 c2 c3
0 0 1 4 7
1 1 2 5 8
2 2 3 6 9
0 1 2 3
0 c0 c1 c2 c3
1 0 1 4 7
2 1 2 5 8
3 2 3 6 9
c0 c1 c2 c3
0 0 1 4 7
1 1 2 5 8
2 2 3 6 9
c1 c2 c3
c0
0 1 4 7
1 2 5 8
2 3 6 9
# -*- coding: utf-8 -*-
import pandas as pd
# read_excel() 함수로 데이터프레임 변환
df1 = pd.read_excel('./남북한발전전력량.xlsx', engine='openpyxl') # header=0 (default 옵션)
df2 = pd.read_excel('./남북한발전전력량.xlsx', engine='openpyxl',
header=None) # header=None 옵션
# 데이터프레임 출력
print(df1)
print('\n')
print(df2)
json 파일 읽기
# -*- coding: utf-8 -*-
import pandas as pd
# read_json() 함수로 데이터프레임 변환
df = pd.read_json('./read_json_sample.json')
print(df)
print('\n')
print(df.index)
name year developer opensource
pandas 2008 Wes Mckinneye True
NumPy 2006 Travis Oliphant True
matplotlib 2003 John D. Hunter True
Index(['pandas', 'NumPy', 'matplotlib'], dtype='object')
저장
# -*- coding: utf-8 -*-
import pandas as pd
# 판다스 DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
data = {'name' : [ 'Jerry', 'Riah', 'Paul'],
'algol' : [ "A", "A+", "B"],
'basic' : [ "C", "B", "B+"],
'c++' : [ "B+", "C", "C+"],
}
df = pd.DataFrame(data)
df.set_index('name', inplace=True) #name 열을 인덱스로 지정
print(df)
# to_csv() 메소드를 사용하여 CSV 파일로 내보내기. 파열명은 df_sample.csv로 저장
df.to_csv("./df_sample.csv")
algol basic c++
name
Jerry A C B+
Riah A+ B C
Paul B B+ C+
# -*- coding: utf-8 -*-
import pandas as pd
# 판다스 DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
data = {'name' : [ 'Jerry', 'Riah', 'Paul'],
'algol' : [ "A", "A+", "B"],
'basic' : [ "C", "B", "B+"],
'c++' : [ "B+", "C", "C+"],
}
df = pd.DataFrame(data)
df.set_index('name', inplace=True) #name 열을 인덱스로 지정
print(df)
# to_json() 메소드를 사용하여 JSON 파일로 내보내기. 파열명은 df_sample.json로 저장
df.to_json("./df_sample.json")
algol basic c++
name
Jerry A C B+
Riah A+ B C
Paul B B+ C+
# -*- coding: utf-8 -*-
import pandas as pd
# 판다스 DataFrame() 함수로 데이터프레임 변환. 변수 df에 저장
data = {'name' : [ 'Jerry', 'Riah', 'Paul'],
'algol' : [ "A", "A+", "B"],
'basic' : [ "C", "B", "B+"],
'c++' : [ "B+", "C", "C+"],
}
df = pd.DataFrame(data)
df.set_index('name', inplace=True) #name 열을 인덱스로 지정
print(df)
# to_excel() 메소드를 사용하여 엑셀 파일로 내보내기. 파열명은 df_sample.xlsx로 저장
df.to_excel("./df_sample.xlsx")