공부중/python

파이썬 입문 6일차

또돌근 2021. 2. 18. 01:15
반응형

 

 

1. csv _1

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 09:47:40 2021

@author: 2016005

csvex1.py : csv 파일 읽기 => 파일 복사하기.

    jeju1.csv파일을 같은 폴더에 넣기
    메뉴-run>configuration per file -> command line options 에 체크
                                        jeju1.csv jeju1_bak.csv    입력
                                        원본파일    복사본파일
"""

import sys   # command로 부터 입력받기 위한 모듈 : sys
'''
runfile('C:/Users/2016005/.spyder-py3/python/workspace/20210204/csvex1.py', 
        args='jeju1.csv jeju1_bak.csv', 
        wdir='C:/Users/2016005/.spyder-py3/python/workspace/20210204')

csvex1.py 가 0
jeju1.csv가 1
jeju1_bak.csv가 2   이렇게 순서임.★ 헷갈리니 기억하기.↓
'''
input_file = sys.argv[1]  #얘는 왜 1부터 시작? 
output_file = sys.argv[2]

with open(input_file, 'r', newline="", encoding="utf8") as filereader:  #내가 전부다 filereader로 만들거야.
    with open(output_file,'w',newline="",encoding="utf8") as filewriter:  #앞의 내용들을 다 filewriter로 만들어줌
        
        header = filereader.readline() # 한줄읽기
        print(type(header))
        
        header = header.strip() # 공백제거 : 화이트스페이스 : 의미없는 공백 제거
        print(header)
        
        header_list = header.split(",")  # , 기준 문자열 분리(csv파일 특징상 , 를 기준으로 '셀' 이 분리됨 )
        print(header_list)
        
        
        #map(str, header_list) : header_list 리스트의 요소들을 문자열로 변환해서 다시한번 리스트로 만들어, 
        # 그것을 , 로 묶어. =>헤더정보를 , 로 만들어서 일반적인 문자열 형태로 만들어 준 것.
        filewriter.write(",".join(map(str,header_list))+"\r\n")
        #                         map : 재배치  : header_list의 요소들을 문자열로 만들어
        #                          문자열 형태가 아닐지라도 문자열로 바꿔줘 라는 것.
        
        for row_list in filereader:  #filereader(입력스트림)으로부터 파일을 한줄씩 읽어서
            filewriter.write(row_list)  # filewriter로 row_lsit(문자열임.)을 쓴다.
            
            
    '''        
#판다스 :
를 이용해서 csv파일을 읽어내는것을 위한 예제.



'''

2. csv _ 2

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 10:14:37 2021

@author: 2016005

csvex2.py : 파일을 읽기 위한 모듈 codecs 사용

codecs를 이용하면 한번에 읽어옴.

판다스 : 파이썬에서 제공하는 '표'
"""

import codecs

filename="jeju1.csv"

csv = codecs.open(filename, "r", "utf8").read()  # 한번에 내용이 다 들어옴.

data = []  # 한줄을   ,  로 분리한 내용을 배열로 저장하고있음.

rows = csv.split("\r\n")  # line별로 나눠서 . 리스트로 저장

for row in rows :
    if row == "":
        continue #비어있으면 넘어가
    cells = row.split(",")  # , 로 분리해서 data에 append하는데, data는 ↑
    data.append(cells)
    
outfp = open("jeju1.txt", "w", encoding='UTF8')

for c in data:
    print(c[0],c[1],c[2]) # 세개.
    outfp.write(" ".join(map(str,c))+"\n") #컴마가 아닌 공백으로 만들어서 파일 output함.
outfp.flush()
outfp.close()

3. pandas _ 1

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 10:40:18 2021

@author: 2016005

pandasex1.py : pandas를 이용하여 csv파일 읽기★
"""

import pandas as pd


infile = "jeju1.csv"
df = pd.read_csv(infile)   # 헤더를 알아서 열로 인식.
print(df)
print(type(df))  #<class 'pandas.core.frame.DataFrame'> 데이터프레임 :이 자체가 표임. 그래서 열, 행 어느 하나만 끄집어낼 수 있는 것.


#내가 경도부분만 출력하겠다
print(df["LON"])

#위도만 출력
print(df["LAT"])

#장소만 출력
print(df["장소"])


#판다스는 헐... 쩐다...
# 표로 만들어준다 = 행과 열로 이뤄져있기때문에 분리 출력해준다는 것.

#print(pd.read_csv("jeju1.csv"))  #이거 딱 한줄이면 그냥 읽어버림.


# 3행만 출력
print(df.iloc[3])  # ==> 컬럼명   내용 \n 컬럼명    내용 ..... 이렇게 나옴.




'''

제일 윗단 헤더야 라는것을자동인식
각각의 행들을 0행 1행 2행... .으로 인식하더라.

csv파일을 엑셀로 읽어내듯이 판다스가 읽어준다. 
컴마 없다. : 컴마기준으로 자기가 분리 읽어냈기때문이다.

             장소         LON        LAT
0        제주국제공항  126.496217  33.505314
1           돔베돈  126.526687  33.516084
2          공룡랜드  126.433153  33.442382
3        협재해수욕장  126.240463  33.404218
4          한림공원  126.239262  33.390101
5          유리의성  126.273744  33.315127
6       오설록티뮤지엄  126.289451  33.306537
7       테디베어뮤지엄  126.408691  33.267972
8       저녁_오성식당  126.414549  33.256059
9        중문관광단지  126.412361  33.249186
10  숙소_서귀포KAL호텔  126.580954  33.248020
'''

4. pandas _ 2

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 10:48:12 2021

@author: 2016005

pandasex2.py : pandas 이해하기
"""

import pandas as pd

df = pd.DataFrame({"A":[1,4,7], "B":[2,5,8], "C":[3,6,9]})   # 딕셔너리인데? 
#                 컬럼명:[행0,행1,행2]
print(df)

#열 선택
print(df["A"])


#행 선택★
'''
행선택 명령
1) iloc[인덱스숫자] : 해당 인덱스 기준 열값 조회
2) loc[행의 이름] : 행의 이름으로 기준, 열값 조회

'''
print("행의 인덱스 값으로 조회하기")

print("df.iloc[0]=",df.iloc[0])#()는 객체값이고, []는 요소의 값.
print("df.iloc[1]=",df.iloc[1])
print("df.iloc[2]=",df.iloc[2])

print("df.loc[0]=",df.loc[0])#()는 객체값이고, []는 요소의 값.
print("df.loc[1]=",df.loc[1])
print("df.loc[2]=",df.loc[2])

###############################################
df = pd.DataFrame(data = ([1,2,3],[4,5,6],[7,8,9]), index=[2,"A",4], columns = [51,52,54])
                 #data = [내용들](한 행이[]한개.)  , index=[행 이름들], columns=[열 이름들]
print(df)


print(df.iloc[2])
print(df.loc[2])

##  iloc[] : 행 숫자로
##  loc[]  : 행 이름으로

5. pandas _ 3

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 11:10:51 2021

@author: 2016005

pandasex3.py : supplier_data.csv 파일을 pandas를 이용해 읽고, 화면에 출력
"""

import pandas as pd

infile="supplier_data.csv"
df = pd.read_csv(infile)
print(df)


#특정 날짜만 골라서 뽑아내기
importdate = ["1/20/14","1/30/14"]
#isin(내 조건): 내가 준 조건값에 맞는지 틀린지를 boolean으로 리턴.
print(df["Purchase Date"].isin(importdate))  # isin : 이즈인.

df_inset = df.loc[df["Purchase Date"].isin(importdate),:] #true인것들만 뽑아냄
#          df.loc[true,:]
#          df.loc[행,:열]
#       loc는 인덱스, 이름, boolean까지도 해줌.

print("df_inset출력 : ",df_inset) # 조건에 맞는 내용들을 다 머금고 있다. 데이터프레임타입임.★
print(type(df_inset))


#invoice가 920으로 시작하는 것들만 뽑기
print("#invoice가 920으로 시작하는 것들만 뽑기")
print(df["Invoice Number"].str.startswith("920-"))  # .str 하면 문자열로 되는가봄.. 그래서 메서드 이용.
print(df.loc[df["Invoice Number"].str.startswith("920-"),:]) # 조건을 변수로 해서 넣어도 됨.


#pandas 데이터를 csv파일로 생성★★★★★
df_inset.to_csv("pandas_out3.csv", index=False) # 인덱스는 제외하고 파일로 만들어.

6. pandas _ 4

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 11:49:37 2021

@author: 2016005

df.loc[행,:열]  열이 아무것도 없으면 다 가져와

pandasex4.py : pandas 파일의 행과 열을 선택하기
"""

import pandas as pd
infile = "supplier_data.csv"
df = pd.read_csv(infile)

df_col = df.iloc[:,[0,3]]  # 0,3번 열의 모든 행을 조회   (한 결과가 df_col에 들어감)
print("df.iloc[:,[0,3]]=>")
print(df_col)

df_col = df.iloc[0:4,0:3] # 0~3번 행, 0~3번 열
print("df.iloc[0:4,0:3]=>")
print(df_col)


#문제:
#Invoice Number값이 920- 로 시작하는 행의 Invoice Number, Cost 컬럼만 조회하기
#조건을 만족하는 데이터를 pandas_out4.csv파일로 저장하기

inset = df["Invoice Number"].str.startswith("920-")

#           [행,     열]    #인덱스, 조건, 이름, n:n / s,s
out = df.loc[inset,["Invoice Number", "Cost"]]
print(out)

out.to_csv("pandas_out4.csv", index=False)

7. excel _ 1

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 12:13:27 2021

@author: 2016005

excelex1.py : xlsx 파일 읽기

****엑셀파일은 xls과 xlsx이 읽는 방법이 다르다. ****
xlsx : import openpyxl
xls  : 

"""
#만약 openpyxl 가 모듈이 설치가 되지 않았다면
#pip3 install openpyxl을 해주면 된다. 

#판다스를 쓰지않고 엑셀파일 읽어내는 방법
import openpyxl

filename="sales_2015.xlsx"
book = openpyxl.load_workbook(filename) #book파일 자체가 엑셀파일이 된다.

'''
엑셀파일과 csv파일의 차이점
엑셀파일은 sheet 개념이 있음.


'''

sheet = book.worksheets[0] # 첫번째sheet를 가져온다. ★
data = []
for row in sheet.rows: # 각각의 행들을 row로 넣어준다. 
    line = []
    for l,d in enumerate(row):  # enumerate를 이용해서 line에 하나씩 넣어준다. row도 list임.
        line.append(d.value)    # 인덱스와 데이터를 나눠서 append
    #print(line)
    data.append(line)           # line은 하나의 row에 각각의 셀들을 list로 했고, data는 그 행의 모임.
#print(data)


#enumerate : 리스트 형태에서 1)데이터와 2)index값을 제공함. 
# (기존은 for i in range(0,len(data))) 했었음.
for i in range(0, len(data)):
    print(i+1,":",data[i])

print("enumerate 함수 사용↓(forEach문 처럼 사용하고싶으면")
for i,d in enumerate(data):
    print(i+1,":",d)

'''
['Customer ID', 'Customer Name', 'Invoice Number', 'Sale Amount', 'Purchase Date'] #헤더정보
[1234, 'John Smith', '100-0002', 123, datetime.datetime(2015, 1, 1, 0, 0)] #date를 이런형식으로 주네.
[2345, 'Mary Harrison', '100-0003', 543, datetime.datetime(2015, 1, 6, 0, 0)] # 문자열은 ''로
[3456, 'Lucy Gomez', '100-0004', 567, datetime.datetime(2015, 1, 11, 0, 0)]
[4567, 'Rupert Jones', '100-0005', 978, datetime.datetime(2015, 1, 18, 0, 0)]
[5678, 'Jenny Walters', '100-0006', 345, datetime.datetime(2015, 1, 24, 0, 0)]
[6789, 'Samantha Donaldson', '100-0007', 645, datetime.datetime(2015, 1, 31, 0, 0)]
'''

8. excel _ 2

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 13:44:00 2021

@author: 2016005

excelex2.py : xlsx파일의 모든 sheet 데이터 읽기

** openpyxl은 최신버전의 엑셀파일확장자를 읽을 때 쓰는 모듈이고
xls구버전 읽을때는 다른 모듈 써야함.
"""

import openpyxl

filename = "sales_2015.xlsx"

book = openpyxl.load_workbook(filename)


###--------------------------------------------------------------------------------###
for i, sheet in enumerate(book.worksheets): #.worksheets에 [인덱스]가 없으면 전부.
#book.worksheets : excel파일의 모든 sheet를 리턴
    print("===========",book.sheetnames[i],"=========") #sheet의 이름을 출력하고
   
    data = []
    for r,row in enumerate(sheet.rows): #rows : 해당 sheet에 있는 행 들을 list형태로 들고와서
        line = []
        for i, c in enumerate(row):  # 인덱스와 데이터를 분리시키고
            line.append(c.value)     #데이터를 line[]에 append시켜준다. 
        print(r+1,":",line)
        data.append(line)
### -------------------------------------------------------------------------------###

9. excel _ 3

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 13:57:47 2021

@author: 2016005

excelex3.py : xls (구버전 엑셀파일) 읽기

    xlsx : openpyxl 모듈 사용
    xls  : xlrd 모듈 사용  => excel파일 읽기 위한 모듈 ('읽기'모듈) 쓰기모듈은 아님.
"""

from xlrd import open_workbook   # from ~ import ~ 와 그냥 import의 차이는??

infile = "ssec1804.xls"

workbook = open_workbook(infile)  #xlrd에 있는 모듈을 이용해서 xls파일 읽어내서 workbook변수로.(이 자체가 xls파일)

print("===sheet의 개수===", workbook.nsheets)

#workbook.sheets() : sheet들의 정보 저장
#worksheet : 한개의 sheet데이터 저장==for문이잖아.
for worksheet in workbook.sheets():
    print("worksheet 이름 : ", worksheet.name)
    print("행의 수 : ", worksheet.nrows)
    print("컬럼의 수 : ", worksheet.ncols)
    
                                            #접근 순서 행-열-셀
    for row_index in range(worksheet.nrows): #1)행 
        for column_index in range(worksheet.ncols):#2)열
            ##worksheet.cell_value(행index, 열index) : #3)셀 데이터
            print(worksheet.cell_value(row_index,column_index),",",end="")
        print()
        

10. excel _ 4

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 14:33:58 2021

@author: 2016005

excelex4.py : xls 파일을 읽고 쓰기
"""

from xlrd import open_workbook  #excel파일 읽기 모듈
from xlwt import Workbook       #excel파일 쓰기 모듈  pip install xlwt(없다면_아나콘다는 자동)

infile = "ssec1804.xls" #원본데이터
outfile = "ssec1804out.xls" #복사본 데이터(하나의 sheet만 저장)

outworkbook = Workbook()  # Workbook() : 비어있는 엑셀파일

out_sheet = outworkbook.add_sheet("전체증감")  #출력할 xls파일에 sheet추가, 이름설정.

# open_workbook(infile) : 원본 데이터 xls파일
with open_workbook(infile) as workbook :
    
    #worksheet : 원본 xls파일 sheet의 이름이 "1.전체증감"의 sheet 데이터.
    worksheet = workbook.sheet_by_name("1.전체증감")
    
    for rindex in range(worksheet.nrows): #행
        for cindex in range(worksheet.ncols): #열
   #out_sheet.write(rindex, cindex, 원본cell) : 원본cell의 데이터를 rindex, cindex에 그대로 저장해.
            out_sheet.write(rindex,cindex, worksheet.cell_value(rindex,cindex)) #셀
            print(worksheet.cell_value(rindex,cindex))
            
            #다 모았어. 그래서 한번에 파일로 저장.
outworkbook.save(outfile) #빈껍데기 엑셀파일에 채워넣고 outfile의 이름으로 저장.

11. excel _ 5

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 15:32:15 2021

@author: 2016005

excelex5.py : pandas 를 이용해서 excel파일 읽고  ,쓰기

"""

import pandas as pd

infile = "sales_2015.xlsx"
outfile = "sales_2015_pd.xlsx"

#read_excel(file이름, "sheet이름", index_col= None인덱스컬럼여부)
df = pd.read_excel(infile, "january_2015",index_col = None)
print(df)

#df_value : Sale Amount 컬럼의 값이 500.0보다 큰 값을 가지는 행을 조회
df_value = df[df["Sale Amount"].astype(float) > 500.0]

#xlsx파일로 저장
#writer : sales_2015_pd.xlsx 파일을 출력파일로 '일단 설정,지정만 해놓은 상태' openpyxl => xlsx형태
writer = pd.ExcelWriter(outfile, engine="openpyxl") # 내가 판다스로부터 outfile이름으로 엔진은 openpyxl모듈로

#writer에다가 df_value의 내용을 저장할거야. 시트이름은 "jan_15_out"으로 하고, 인덱스값은 없어.
# df_value의 데이터를 writer파일에 "jan_15_out"시트 이름으로 저장(인덱스 표시 x)
df_value.to_excel(writer, sheet_name="jan_15_out", index=False)
#writer에 저장된 상태.
#다른시트이름으로 추가해서 저장도 가능.

writer.save()
#마무리. 파일생성.

12. excel _ 6

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 15:49:41 2021

@author: 2016005

excelex6.py : pandas를 이용해서 xlsx파일 읽기(여러개의 시트 다 읽기)
    1. xlsx 파일의 모든 sheet를 읽기  => 하나의 시트로 합침
"""

import pandas as pd

infile = "sales_2015.xlsx"

df = pd.read_excel(infile, sheet_name=None, index_col = None) 
# 그냥 엑셀을 읽어오는데, 시트이름 지정안하고 인덱스 x

row_output = []
#모든 시트에 있는 데이터들을 전부다 저장하기 위한 변수

# df.items() : 시트정보.
# worksheet_name : sheet이름 임.
# data : sheet 데이터
for worksheet_name, data in df.items():  #데이터프레임형태로 들어옴.
    print("====",worksheet_name,"====")
    row_output.append(data[data["Sale Amount"].replace("$","").replace(",","").astype(float) > 200.0])
    #데이터에 조건을 준 것.(Sales Aoumnt컬럼의 값에 $ , 없애고, float형태로 만들어서 200보다 큰 데이터만)
    # astype
    
'''
pd.concat : row_output 은 리스트. sheet에 의해서 모든데이터를 가지고 있는 놈.
           을 붙여서 여러개의 데이터를 연결하는데,

axis 행으로 붙일지 열로 붙일지 결정(0,1)
           axis=1  : column에 연결해
           axis=0  : row에 연결해
'''
filtered_row = pd.concat(row_output, axis=0, ignore_index=True) 

#filtered_row : 모든 sheet에서 조건에 맞는 데이터들을 연결해서 저장한 것..
writer = pd.ExcelWriter("sales_all_2015.xlsx", engine="openpyxl")

#
filtered_row.to_excel(writer, sheet_name="sale_2015", index=False)
writer.save()

===========exam============

1. exam _ 1

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 14:50:50 2021

@author: 2016005

exam1.py : ssec1804.xls 파일에서 1.남자, 1.여자 sheet의 데이터를
            ssec1804mf.xls파일에 sheet이름 남자,여자 로 데이터 저장하기
"""

from xlrd import open_workbook
from xlwt import Workbook

infile = "ssec1804.xls"
outfile = "ssec1804mf.xls"

outworkbook = Workbook()
'''
out_sheet = outworkbook.add_sheet("남자")
with open_workbook(infile) as workbook:
    worksheet = workbook.sheet_by_name("1.남자")
    for rindex in range(worksheet.nrows):
        for cindex in range(worksheet.ncols):
            out_sheet.write(rindex, cindex, worksheet.cell_value(rindex,cindex))
            print(out_sheet)
            
out_sheet = outworkbook.add_sheet("여자")
with open_workbook(infile) as workbook:
    worksheet = workbook.sheet_by_name("1.여자")
    for rindex in range(worksheet.nrows):
        for cindex in range(worksheet.ncols):
            out_sheet.write(rindex, cindex, worksheet.cell_value(rindex,cindex))
            print(out_sheet)
outworkbook.save(outfile)
'''
'''
sheets = ["남자","여자"]
for i in range(0,len(sheets)):
    out_sheet = outworkbook.add_sheet(sheets[i])
    with open_workbook(infile) as workbook:
        worksheet = workbook.sheet_by_name("1."+sheets[i])
        for rindex in range(worksheet.nrows):
            for cindex in range(worksheet.ncols):
                out_sheet.write(rindex, cindex, worksheet.cell_value(rindex,cindex))
                print(out_sheet)
outworkbook.save(outfile)
'''

##선생님 코드(def이용한 방법)##
def makesheet(output_sheet):
    for row_index in range(worksheet.nrows):
        for column_index in range(worksheet.ncols):
            output_sheet.write(row_index, column_index, worksheet.cell_value(row_index, column_index))
            print(worksheet.cell_value(row_index, column_index))


infile = "ssec1804.xls"
outfile = "ssec1804mf.xls"

worksheet = None  # 전역변수로 선언함.

outworkbook = Workbook() # 빈껍데기. 출력될 xls파일의 내용

# sheet추가
output_sheet_male = outworkbook.add_sheet("남자")
output_sheet_female = outworkbook.add_sheet("여자")


with open_workbook(infile) as workbook:  #workbook : ssec1804.xls의 모든 데이터
    
    worksheet = workbook.sheet_by_name("1.남자")  #worksheet : 1.남자 sheet데이터. 이 내용을 가지고 def실행
    makesheet(output_sheet_male)  # 남자 시트에 대한 작업 시행
    
    worksheet = workbook.sheet_by_name("1.여자")
    makesheet(output_sheet_female)
    
outworkbook.save(outfile)
    

2. exam _ 2

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 16:08:41 2021

@author: 2016005

exam2.py : sales_2015.xlsx 파일에서 
            sheet=january_2015 인 sheet의 Customer Name컬럼의 값이 J로 시작하는 행만
            선택해서 sale_2015_J.xlsx 파일로 저장하기
            시트이름은 똑같이.
"""

import pandas as pd

infile = "sales_2015.xlsx"

df = pd.read_excel(infile, "january_2015", index_col = None) 
row_output= df[df["Customer Name"].str.startswith("J")]
writer = pd.ExcelWriter("sales_2015_J.xlsx", engine="openpyxl")
row_output.to_excel(writer, sheet_name="sales_2015_J", index=False)

writer.save()


'''

'''

===========test=============

1. test _ 1

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 16:50:19 2021

@author: 2016005

test1

1. sales_2013.xlsx 파일 중 
Purchase Date 컬럼의 값이 
"01/24/2013"과 "01/31/2013" 인 행만 sales_2013_01.xlsx 파일로 저장하기  

2. sales_2013.xlsx 파일의 
january_2013 sheet의 중 
열이 "Customer Name", "Sale Amount" 컬럼만  sales_2013_amt.xlsx 파일로 저장하기 

3. sales_2013.xlsx 파일의 
모든 sheet의  열이 "Customer Name", "Sale Amount" 컬럼만 
sales_2013_allamt.xlsx 파일로 저장하기 
"""

import pandas as pd

infile = "sales_2013.xlsx"

df = pd.read_excel(infile, sheet_name=None, index_col = None)

row_output = []

cond = ["01/24/2013","01/31/2013"] ##조건이 두개면, list로 넣고 isin을 쓴다.

for worksheet_name, data in df.items():  #데이터프레임형태로 들어옴.
    print("====",worksheet_name,"====")
    print(data[data["Purchase Date"].isin(cond)])
    row_output.append(data[data["Purchase Date"].isin(cond)])
    
filtered_row = pd.concat(row_output, axis=0, ignore_index=True) 
writer = pd.ExcelWriter("sales_2013_01.xlsx", engine="openpyxl")

filtered_row.to_excel(writer, sheet_name="sale_2013", index=False)
writer.save()

2. test _ 2

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 17:31:20 2021

@author: 2016005

test2

2. sales_2013.xlsx 파일의 
january_2013 sheet 중 
열이 "Customer Name", "Sale Amount" 컬럼만  sales_2013_amt.xlsx 파일로 저장하기 

--->pandas에서 컬럼만 선택
"""

import pandas as pd

infile = "sales_2013.xlsx"

df = pd.read_excel(infile, "january_2013", index_col=None)

df_value = df.loc[:,["Customer Name","Sale Amount"]]
print(df_value)


#Excelwriter 설정
writer = pd.ExcelWriter("sales_2013_amt.xlsx", engine="openpyxl")

#to_excel(설정한writer, 시트이름, 인덱스표시여부)
df_value.to_excel(writer, sheet_name="january_2013",index=False)

#파일 저장
writer.save()

3. test _ 3

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 15 17:49:13 2021

@author: 2016005

test3


3. sales_2013.xlsx 파일의 
모든 sheet의  열이 "Customer Name", "Sale Amount" 컬럼만 
sales_2013_allamt.xlsx 파일로 저장하기 

"""

import pandas as pd

infile = "sales_2013.xlsx"

df = pd.read_excel(infile, sheet_name=None, index_col=None)

df_value = []

for sheetname, data in df.items():
    print(sheetname)
    #print(data.loc[:,["Customer Name","Sale Amount"]])
    df_value.append(data.loc[:,["Customer Name","Sale Amount"]])

print(df_value)
    
filtered_row = pd.concat(df_value,axis=0,ignore_index=False)

writer = pd.ExcelWriter("sales_2013_allamt.xlsx", engine="openpyxl")

filtered_row.to_excel(writer,sheet_name="sales_2013_allamt",index=False)

writer.save()

4. test _ 4

# -*- coding: utf-8 -*-
"""
Created on Tue Feb 16 09:44:23 2021

@author: 2016005

test4.py : sales_2013.xlsx 파일의 모든 sheet의
열이 "Customer Name", "Sale Amount"컬럼만
sales_2013_alamtxlsx파일로 저장(시트는 그대로 각각 시트에 저장)
"""

import pandas as pd
infile = "sales_2013.xlsx"
outfile = "sales_2013_allamt.xlsx"

writer = pd.ExcelWriter(outfile)

df = pd.read_excel(infile, sheet_name=None, index_col = None)

for worksheet_name, data in df.items():
    #data: 각 sheet의 데이터 저장 변수. for문이다.
    #worksheet_name : 각sheet의 이름
    print("===",worksheet_name,"===")
    data_value = data.loc[:,["Customer Name","Sale Amount"]]
    
    #writer : excel파일
    #to_excel() : 읽어온 sheet를 추가한다(writer에. )
    data_value.to_excel(writer, sheet_name=worksheet_name,index=False)
    #sheet를 그대로 들고오는 방법↑(for문 안에서 to_excel한다.)
writer.save()
반응형

'공부중 > python' 카테고리의 다른 글

파이썬 입문 7일차  (0) 2021.02.18
파이썬 입문 5일차  (0) 2021.02.15
파이썬 입문 3일차  (0) 2021.02.08
파이썬 입문 2일차  (0) 2021.02.08
파이썬 입문 1일차  (0) 2021.02.08