본문 바로가기

Study/Python28

[Python] Matplotlib In [1]: import matplotlib.pyplot as plt In [2]: y = [2, 4, 6] # plot - 선 그래프(Line plot) plt.plot(y) plt.show() In [3]: y = [2, 4, 6] x = [1, 2, 3] # plot - 선 그래프(Line plot) plt.plot(x, y) # x 와 y의 갯수가 맞지 않으면 error plt.show() In [4]: # line style -> ls plt.plot(x,y, ls = '--') plt.show() In [5]: # Marker plt.plot(x, y, marker='o') plt.show() In [6]: x = [2, 4, 6, 8] y = [10, 13, 16, 20] y2 = [10,.. 2023. 10. 16.
[Python] pandas라이브러리 CrimeData 실습 import pandas as pd In [2]: c15 = pd.read_csv('crime/2015.csv', encoding = 'euc-kr', index_col='관서명') c16 = pd.read_csv('crime/2016.csv', encoding = 'euc-kr', index_col='관서명') c17 = pd.read_csv('crime/2017.csv', encoding = 'euc-kr', index_col='관서명') c17 Out[2]: In [3]: total15 = c15.loc[c15['구분'] == '발생건수','살인':].sum(axis=1) total15 Out[3]: 관서명 광주지방경찰청계 18830 광주동부경찰서 2355 광주서부경찰서 4720 광주남부경찰서 21.. 2023. 10. 13.
[Python] 함수 함수 def 함수명(매개변수):실행문장return 반환변수 -> 호출했던 곳으로 감 In [1]: def number_sum(num1, num2): r = num1 + num2 return r # 함수 정의 후 실행 시켰는지 확인하기 In [2]: print(number_sum(2,3)) 5 # 실습문제 In [3]: def cal(num1, num2, op='+') : # op='+' 매개변수 op의 기본값을 + 로 설정 """덧셈과 뺄셈을 계산하는 함수""" # docstring 추가하기 if op == '+' : result = num1 + num2 return result elif op == '-' : result = num1 - num2 return result else : return '연산기.. 2023. 10. 12.
[Python] 딕셔너리 딕셔너리 In [1]: # 딕셔너리 생성 # 딕셔너리명 = {Key:value,Key:value , , } # key값은 변하지 않을 값, 동일한 key는 여러개가 올 수 없다 a = {} b = {'name':'youngD', 'pn':'010-1234-5678'} c = {1 : 'youngD', 2:'oldD'} print(type(a)) print(a) {} In [2]: # 딕셔너리 값 추가 dic1 = {'name':'youngD', 'age':20, 'phone': '010-1234-5678'} dic1['birth'] = '07/05' # 딕셔너리 값 변경 dic1['name'] = 'oldD' print(dic1) {'name': 'oldD', 'age': 20, 'phone': '010.. 2023. 10. 11.