[Python/Pandas] 데이터 순위를 정하는 Pie 차트개발
일상에서도 많이 쓰이는 pie차트 개발을 위한 코드 및 설명 전체 코드
matplotlib, pandas 임폴트 하기
matplotlib 는 차트개발을 위한 모듈
pandas 는 데이터 분석 가공을 위한 모듈
import matplotlib.pyplot as plt
import pandas as pd
matplotlib 차트 에서 한글폰트 사용설정하기
#한글폰트 사용하기, 중요! 한글폰트가 사용된 plt.show() 함수보다 앞에 위치 시켜야 함
plt.rc('font', family="NanumGothic")
pandas 로 데이터 가공하기
데이터 불러오기
pd.read_csv()
file = "./data/data_2020.csv"
data = pd.read_csv(file)
불러온 데이터 확인하기
.head
data_Head = data.head() #데이터 모양 확인하기
print(data_Head)
집계일자 집계시 출발영업소코드 도착영업소코드 통행시간 요일
0 20200101 4 101 105 637 2
1 20200101 4 101 105 773 2
2 20200101 4 101 105 762 2
3 20200101 4 101 105 746 2
4 20200101 4 101 105 875 2
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 152870 entries, 0 to 152869
불러온 데이터 프레임 구조 보기
info()
data_Info = data.info() #데이터 프레임 구조보기
print(data_Info)
Data columns (total 6 columns):
집계일자 152870 non-null int64
집계시 152870 non-null int64
출발영업소코드 152870 non-null int64
도착영업소코드 152870 non-null int64
통행시간 152870 non-null int64
요일 152870 non-null int64
dtypes: int64(6)
memory usage: 7.0 MB
None
선택자
isin()
정렬하기
sort_vallues(by=)
data_06_22 = data[data.집계시.isin(range(6,23))] #집계시 6시부터 ~ 22시까지 만 불러오기
print(data_06_22.sort_values(by='집계시')) #시간별로 정렬해서 출력한다.
집계일자 집계시 출발영업소코드 도착영업소코드 통행시간 요일
82028 20200715 6 101 140 16775 2
125225 20201026 6 101 110 5101 0
66049 20200609 6 101 105 713 1
66048 20200609 6 101 105 816 1
76951 20200704 6 101 135 10050 5
66047 20200609 6 101 105 894 1
141023 20201203 6 101 110 3452 3
66046 20200609 6 101 105 826 1
76952 20200704 6 101 135 12397 5
66045 20200609 6 101 105 832 1
66044 20200609 6 101 105 657 1
125235 20201016 6 101 120 6121 4
125249 20201016 6 101 115 5465 4
42786 20200414 6 101 110 2774 1
42787 20200414 6 101 110 3214 1
42788 20200414 6 101 110 3029 1
42789 20200414 6 101 110 3402 1
95557 20200818 6 101 120 8515 1
118321 20201009 6 101 110 3231 4
42798 20200412 6 101 110 3245 6
66003 20200607 6 101 140 17323 6
42800 20200414 6 101 115 4739 1
132441 20201114 6 101 130 9165 5
42801 20200414 6 101 115 5841 1
118330 20201001 6 101 115 4683 3
140981 20201202 6 101 125 7399 2
28257 20200310 6 101 110 2909 1
28256 20200310 6 101 110 3544 1
28255 20200310 6 101 110 3758 1
28254 20200310 6 101 110 3192 1
... ... ... ... ... ... ..
38817 20200404 22 101 125 8610 5
12583 20200131 22 101 120 6872 4
74023 20200627 22 101 135 16289 5
74032 20200627 22 101 140 16666 5
22476 20200224 22 101 140 14877 0
107492 20200915 22 101 115 5738 1
107491 20200915 22 101 115 6099 1
107490 20200915 22 101 115 4808 1
38788 20200404 22 101 120 7679 5
38779 20200404 22 101 115 5592 5
74067 20200626 22 101 110 3620 4
38778 20200404 22 101 115 5249 5
74178 20200628 22 101 105 816 6
38777 20200404 22 101 115 4666 5
38776 20200404 22 101 115 5434 5
5149 20200113 22 101 125 9153 0
12567 20200131 22 101 115 5756 4
107406 20200915 22 101 110 4041 1
107405 20200915 22 101 110 3112 1
12566 20200131 22 101 115 5805 4
12565 20200131 22 101 115 5851 4
12564 20200131 22 101 115 4886 4
22536 20200214 22 101 115 6305 4
107367 20200913 22 101 110 3548 6
5180 20200113 22 101 135 14165 0
74174 20200628 22 101 105 765 6
74175 20200628 22 101 105 870 6
74176 20200628 22 101 105 849 6
107489 20200915 22 101 115 6371 1
7741 20200120 22 101 105 873 0
카운트 하기
.value_counts()
data_06_22_counting = data_06_22['집계시'].value_counts() #시간별로 카운트하기
print(data_06_22_counting)
[115244 rows x 6 columns]
13 7092
14 7091
12 7089
15 7078
11 7059
10 7038
16 7011
9 7008
17 6919
8 6849
18 6742
19 6627
7 6614
20 6518
21 6284
22 6130
6 6095
Name: 집계시, dtype: int64
인덱스 데이터 가공
.index
x = data_06_22_counting.index #x 축에 index 시간별로 저장하기
labels = [str(i)+'시' for i in x] #인덱스 틀어짐 방지하기 위해 str 타입으로 변환해서 저장
print(labels)
['13시', '14시', '12시', '15시', '11시', '10시', '16시', '9시', '17시', '8시', '18시', '19시', '7시', '20시', '21시', '22시', '6시']
차트설정하기
explode = [0.1, 0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
values = data_06_22_counting.values
plt.figure(figsize=(7,7))
plt.pie(values, explode=explode, labels=labels, startangle= 90, shadow=True, autopct='%.1f')
plt.title("집계시 기준 통행회수", fontsize=18)
plt.show()
전체코드
import matplotlib.pyplot as plt
import pandas as pd
#한글폰트 사용하기, 중요! 한글폰트가 사용된 plt.show() 함수보다 앞에 위치 시켜야 함
plt.rc('font', family="NanumGothic")
file = "./data/data_2020.csv"
data = pd.read_csv(file) #데이터 읽어와 객체에 저장하기
data_Head = data.head() #데이터 모양 확인하기
print(data_Head)
data_Info = data.info() #데이터 프레임 구조보기
print(data_Info)
data_06_22 = data[data.집계시.isin(range(6,23))] #집계시 6시부터 ~ 22시까지 만 불러오기
print(data_06_22.sort_values(by='집계시')) #시간별로 정렬해서 출력한다.
data_06_22_counting = data_06_22['집계시'].value_counts() #시간별로 카운트하기
print(data_06_22_counting)
x = data_06_22_counting.index #x 축에 index 시간별로 저장하기
labels = [str(i)+'시' for i in x] #인덱스 틀어짐 방지하기 위해 str 타입으로 변환해서 저장
print(labels)
explode = [0.1, 0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
values = data_06_22_counting.values
plt.figure(figsize=(7,7))
plt.pie(values, explode=explode, labels=labels, startangle= 90, shadow=True, autopct='%.1f')
plt.title("집계시 기준 통행회수", fontsize=18)
plt.show()
다른 방식으로 pie 차트 구현해보기
데이터 중 도착영업소 코드 를 불러와 value_counts() 를 이용해 도착영업소별 통행회수를 카운트 한 값을 data_destination_counting 에 담아 출력한다.
data_destination_counting = data['도착영업소코드'].value_counts()
print(data_destination_counting)
105 49738
110 38526
115 26354
140 8954
130 7793
135 7428
125 7339
120 6738
Name: 도착영업소코드, dtype: int64
data_destination_counting 인덱스인 도착영업소 코드를 labels에 담아 출력한다.
x = data_destination_counting.index
labels = [str(i) for i in x]
print(labels)
['105', '110', '115', '140', '130', '135', '125', '120']
labels 에 도착영업소코드를 각 코드에 맞는 도착영업소로 변경한다.
파이차트 explode 를 설정한다. 소수점 설정으로 차트가 떨어지는 효과
values 에 data_destination_counting벨류 값을 담는다.
차트를 표현 한다.
labels = ['기흥', '목천', '대전', '부산', '동김천', '경주', '남구미', '황간']
explode = [0.2, 0.1, 0, 0, 0, 0, 0, 0]
values = data_destination_counting.values
plt.figure(figsize=(7,7))
plt.pie(values, explode=explode, labels=labels, startangle= 90, shadow=True, autopct='%.1f%%', counterclock=False)
plt.title("도착영업소 기준 통행회수", fontsize=18)
plt.show()
전체코드
import matplotlib.pyplot as plt
import pandas as pd
#한글폰트 사용하기, 중요! 한글폰트가 사용된 plt.show() 함수보다 앞에 위치 시켜야 함
plt.rc('font', family="NanumGothic")
file = "./data/data_2020.csv"
data = pd.read_csv(file) #데이터 읽어와 객체에 저장하기
data_Head = data.head() #데이터 모양 확인하기
print(data_Head)
data_Info = data.info() #데이터 프레임 구조보기
print(data_Info)
data_destination_counting = data['도착영업소코드'].value_counts()
print(data_destination_counting)
x = data_destination_counting.index
labels = [str(i) for i in x]
print(labels)
labels = ['기흥', '목천', '대전', '부산', '동김천', '경주', '남구미', '황간']
explode = [0.2, 0.1, 0, 0, 0, 0, 0, 0]
values = data_destination_counting.values
plt.figure(figsize=(7,7))
plt.pie(values, explode=explode, labels=labels, startangle= 90, shadow=True, autopct='%.1f%%', counterclock=False)
plt.title("도착영업소 기준 통행회수", fontsize=18)
plt.show()
105 49738
110 38526
115 26354
140 8954
130 7793
135 7428
125 7339
120 6738
Name: 도착영업소코드, dtype: int64
['105', '110', '115', '140', '130', '135', '125', '120']
댓글