12월, 2019의 게시물 표시

[파이썬] #12 - pandas 다루기 - 엑셀

[파이썬] #12 - pandas 다루기 - 엑셀 엑셀 모듈 설치 # pip install xlrd xlwt # pip install openpyxl #pandas 모듈을 활용한 엑셀 파일 로딩처리 #pandas 모듈의 read_excel 함수 #read_excel(엑셀파일명, sheetname='읽어올 sheet이름') # -sheetname 생략할경우 첫번째 시트를 선택 # - header 매개변수로 수정가능. # - nrows 몇개의 row를 가지고올지지정 # - index_col 인덱스번호설정 # - isin 메소드 사용 #pandas DataFrame 의 iloc 메소드 #iloc[행의정보(시작인덱스:종료인덱스), [열의정보]] # -*- coding: utf-8 -*- """ Created on Tue Dec 10 19:39:07 2019 # 엑셀 파일 Read, Write 모듈 설치(파이썬) # pip install xlrd xlwt # pip install openpyxl 아나콘다에서 설치할때 #conda install xlrd xlwt #conda install openpyxl """ import pandas as pd input_file = './data/sales_2013.xlsx' #pandas 모듈을 활용한 엑셀 파일 로딩처리 #pandas 모듈의 read_excel 함수 #read_excel(엑셀파일명, sheetname='읽어올 sheet이름') # -sheetname 생략할경우 첫번째 시트를 선택 sales = pd.read_excel(input_file) print(sales) print(sales.info()) #DataFrame 저장된 정보를 엑셀파일로 출력 output_file = './data/sales_2013_temp.xlsx' #엑셀파일로 출력할수 있는 객체 생...

[파이썬] #11 - pandas 다루기 - 기본

 [파이썬] #11 - pandas 다루기 """ Created on Mon Dec 9 20:13:36 2019 pandas 는 데이터 분석을 위해 사용하는 라이브러리 패키지 데이터의 간단한 통계 정보 및 시각화를 위한 다양한 기능제공 - 손쉬운 파일 입출력 - 다양한 파일 포맷 지원 #pip install pandas #conda install pandas """ import pandas as pd #pandas의 데이터 저장구조 # 1차원 : Series # 2차원 : DataFrame # 3차원 : Panel series_data = list(range(1,10,2)) s = pd.Series(series_data) print(f'type(s) -> {type(s)}') print(s) """ Created on Mon Dec 9 20:38:09 2019 """ #DataFrame 생성을 위한 딕셔너리 변수의 선언 #{키1 : 값1, .....} import pandas as pd data = { 'year' : [2017,2018,2019,2020], 'GDP Rate' : [1.8,3.1,3.0,None], 'GDP' : ['1.637M', '1.859M', '2.237M',None] } #딕셔러니 변수를 사용하여 DataFrame 객체 생성, # 각키의 값들은 동일한 개수 # 해당위치에 데이터가 없는경우 결측데이터(NaN) 이 대입. df = pd.DataFrame(data) print(f'type(df) -> {type(df)}') print(df) # -*- coding: utf-8 -*- #DataFrame 생성을 위한 딕셔...

[파이썬] #10 - csv 다루기

[파이썬] #10 - csv 다루기 import csv count = 3 scores = [] for i in range(1, count+1): scores.append(int(input(f"{i}번째 성적:"))) fname = './data/file_08.txt' # newline 옵션 # 개행 문자를 처리하기 위한 옵션 값 # 개행 문자를 newline 매개변수에 전달된 값으로 # 대체해서 출력할 수 있는 옵션 # w, a 모드에 사용 #with을 사용하면 close 안써도된다. with open(fname, 'a', newline='') as output : csv_writer = csv.writer(output) csv_writer.writerow(scores) import csv input_file_name= './data/file_08.txt' with open(input_file_name, 'r') as input_file : #파일을 읽을 수 있는 객체를 사용 # csv 모듈의 reader 객체 생성 reader = csv.reader(input_file) count = 0 for scores_row in reader : count += 1 #scores_row : 100,90,85 -> [100,90,85] print(f"{count}번째 학생 성적 : {scores_row}") import csv input_file_name = './data/file_10.csv' with open(input_file_name, 'r') as input_file : reader = csv.reader(input_file, delimiter=',') #enumera...

[파이썬] #9 - file 다루기

 [파이썬] #9 - file 다루기 #파일처리 #open함수 #open(파일명, 모드[encoding, newline..]) #open 함수의 두번째 매개변수를 w 로 두면 쓰기모드로 동작하여 파일생성, 기존생성된 파일은 삭제되낟. open('./data/file_01.txt', 'w') #파일에 내용추가 output_filename = "./data/file_02.txt" output = open(output_filename, 'w') output.write('파일내용작성ㅎㅎㅎ') #파일 시스템 리소스 접근경우 반드시 close메소드호출필요 output = open(output_filename, 'r') #파일 읽기 fileData = output.read() print(fileData) output.close() #구구단 출력 fname = "./data/file_04.txt" output = open(fname, 'w') output_data = [] for dan in range(2,10) : output_data.append(f"{dan}을 출력합니다") for mul in range(1, 10) : output_data.append(f"{dan} X {mul} = {dan*mul}") output.writelines(map(lambda line : line + '\n', output_data)) output = open(fname, 'r') lines = output.readlines() # input_data 리스트 변수를 map 함수를 사용하여 # 각 리스트 내부의 값들에 포함된 개행문자를 # 제거하세요. lines = map(lambda x : str(x).rstrip() , lines) for l...