파이썬 Python
-
[10일차/파이썬] 복습파이썬 Python/파이썬 2023. 1. 6. 17:26
set을 이용한 중복 방지 로직 import random def is_samenumber(numbers): flag = False str_num = str(numbers) result = set(str_num) # set : 순서 x 중복 x (중복이 있으면 제외하고 넣음) if len(result)==4: flag=True return not flag pandas로 전처리하여 matplotlib으로 그래프 출력 import pandas as pd from matplotlib import pyplot as plt doctor_df = pd.read_csv('./data/12/doctor2.csv') area = doctor_df['지역'].values # value라는 속성을 꺼내옴. 함수 아님. -> ..
-
[9일차/파이썬] ★Pandas파이썬 Python/파이썬 2023. 1. 5. 17:11
import pandas as pd obj = pd.Series([10,20,30,40,50]) print(obj*10) ''' 0 100 1 200 2 300 3 400 4 500 dtype: int64 ''' print(obj>25) ''' 0 False 1 False 2 True 3 True 4 True dtype: bool ''' print(obj[obj>25]) ''' 2 30 3 40 4 50 dtype: int64 ''' Pandas 활용하여 CSV 파일 읽기 import pandas as pd gisa_df = pd.read_csv('./data/Abc1115.csv', header=None) gisa_df.columns = ['std','email','kor','eng','math','s..
-
[9일차/파이썬] Numpy파이썬 Python/파이썬 2023. 1. 5. 15:39
import numpy as np data = np.array([1, 2, 3, 4, 5]) print(type(data)) # print(data.dtype) #int32 print(data) #[1 2 3 4 5] import numpy as np data = np.random.randn(2,3) print(data) print(type(data)) print(data.shape) print(data.dtype) ''' [[ 2.08874584 -0.32466693 0.0936289 ] [-0.30339426 -1.31066591 -0.27167037]] (2, 3) float64 ''' insert import numpy as np x = np.array([[1,1,1],[2,2,2],[3,3,3]..
-
[9일차/파이썬] ★데이터 시각화 연습문제파이썬 Python/파이썬 2023. 1. 5. 11:01
교재 ★ CSV파일을 읽어 서울과 6개 광역시에 대해 갹 의사 수 출력 # p.407 # 문제2 # 나의 코드 file = open('./data/12/doctor_2019.csv','r',encoding='UTF-8') lines = file.readlines() file.close() data=[] for line in lines[1:]: temp = line[:-1].split(',') for i in range(2, len(temp)): temp[i] = int(temp[i]) data.append(temp) seoul=[] busan=[] daegu=[] incheon=[] daejun=[] gwangju=[] ulsan=[] for d in data: if d[0]=='서울': seoul.ap..
-
[8일차/파이썬] ★데이터 시각화 - 연습문제파이썬 Python/파이썬 2023. 1. 4. 17:52
교재 p.406 월별 카페 개수를 나타내는 막대 그래프 # p.406 # 연습문제1 from matplotlib import pyplot as plt file = open('./data/12/cafe_2year.csv','r',encoding='UTF-8') lines = file.readlines() file.close() # 필요한 데이터를 위에서 다 가져왔으므로 파일 닫기 cafe_list=[] for line in lines[1:]: temp = line[:-1].split(',') cafe_list.append(temp) x=[] y=[] count = 0 for data in cafe_list[2:]: if count % 3 == 0: x.append(data[0]) y.append(data[..
-
[8일차/파이썬] 데이터 분석 기초 연습문제파이썬 Python/파이썬 2023. 1. 4. 17:52
교재 전남 지역의 치과병원에 대해 병원명, 주소, 총 의사수의 목록을 출력 # p.373 #문제 1 file = open("./data/11/hospital_2019.csv",'r',encoding="UTF-8") lines = file.readlines() file.close() data = [] for line in lines[1:]: temp = line[:-1].split(',') if ")" in temp[4]: temp[3] = temp[3] + temp[4] temp.remove(temp[4]) data.append(temp) for d in data: if d[2]=='전남' and d[1]=='치과병원': print('{} / {} / {}'.format(d[0],d[3],d[-1])) ..
-
[7일차/파이썬] ★리스트 정렬파이썬 Python/파이썬 2023. 1. 3. 17:51
정렬 로직 list1 = [3,9,1,5,4,7,8] for i in range(len(list1)-1): for j in range(i+1, len(list1)): if list1[i] > list1[j]: temp = list1[i] list1[i] = list1[j] list1[j] = temp print(list1) ''' [1, 3, 4, 5, 7, 8, 9] ''' 파일 읽어와서 총점 오름차순 정렬하기 def get_list2(filepath): file = open(filepath, 'r', encoding="UTF-8") lines = file.readlines() file.close() data = [] for line in lines: temp = line[:-1].split(',') ..
-
[7일차/파이썬] CSV 파일 데이터 전처리파이썬 Python/파이썬 2023. 1. 3. 17:51
파일 속 데이터를 전처리하여 새 파일에 저장 # 파일 속 데이터를 전처리하여 새 파일에 저장 file = open('./data/month_temp.csv','r', encoding='UTF-8') lines = file.readlines() file.close() data = [] for line in lines: temp = line[:-1].split(',') data.append(temp) file = open('month_temp2.csv','w',encoding="UTF-8") file.write('일자,최저기온,최고기온,일교차\n') for d in data[1:]: d[4] = float(d[4]) d[3] = float(d[3]) file.write('{},{},{},{:.1f}\n'.f..