2016-12-04 2 views
0

배경 : 내 첫 번째 Excel 관련 스크립트. openpyxl 사용. 다른 열에 제품에 대한 여러 가지 유형의 데이터가 많은 Excel 시트가 있습니다.내 사전을 Python의 Excel 시트에 출력하는 방법

목표는 특정 열 (예 : 가격, 바코드, 상태)에서 특정 유형의 데이터를 추출하여 고유 한 제품 코드에 할당 한 다음 제품 코드, 가격, 바코드 및 상태를 새로운 Excel 문서로 출력하는 것입니다.

나는 데이터를 추출하고 다음 사전 형식으로 퍼팅에 성공했다 : (나는이 잘못된 것을 알고 있지만)

productData = {'AB123': {'barcode': 123456, 'price': 50, 'status': 'NEW'} 

새 보고서에이 출력을 얻기에 내 일반적인 생각이 같은 것입니다을 :

newReport = openpyxl.Workbook() 
newSheet = newReport.active 
newSheet.title = 'Output' 

newSheet['A1'].value = 'Product Code' 
newSheet['B1'].value = 'Price' 
newSheet['C1'].value = 'Barcode' 
newSheet['D1'].value = 'Status' 

for row in range(2, len(productData) + 1): 
    newSheet['A' + str(row)].value = productData[productCode] 
    newSheet['B' + str(row)].value = productPrice 
    newSheet['C' + str(row)].value = productBarcode 
    newSheet['D' + str(row)].value = productStatus 

newReport.save('ihopethisworks.xlsx') 

실제로 데이터를 출력하려면 어떻게해야합니까?

답변

0

팬더를 사용하는 것이 좋습니다. 구문은 다음과 같습니다.

df = pd.read_excel('your_file.xlsx') 
df['Column name you want'].to_excel('new_file.xlsx') 

더 많은 작업을 수행 할 수 있습니다. Openpyxl은 작업에 적합한 도구가 아닐 수도 있습니다 (Openpyxl은 너무 일반적입니다).

P. 나는 코멘트에 이것을 남겨 둘 것이다. 그러나 stackoverflow는 누군가가 대답을 남기도록하고, 주석을 달지 않기로 결정했다.

+0

입니다. 어쨌든 당신이 제안한 것은 openpyxl'ws [ 'A']' –

0

데이터를 추출하는 데 사용하는 논리가 누락되었지만 가장 좋은 방법은이 방법을 사용하여 두 워크 시트를 병렬로 반복하는 것입니다. 그런 다음 사전을 완전히 사용하지 않고 새로운 워크 시트에 루프를 추가하는 것을 피할 수 있습니다.

의사 코드 :

ws1 # source worksheet 
ws2 # new worksheet 

product = [] 
code = ws1[…] # some lookup 
barcode = ws1[…] 
price = ws1[…] 
status = ws1[…] 

ws2.append([code, price, barcode, status]) 
0

팬더가 여기 에 가장 적합한 그것은 데이터가 컬럼에 있는지 여부를 골대 분명하지 않다 몇 가지 예

import pandas as pd 

#df columns: Date Open High  Low Close  Volume 
#reading data from an excel 
df = pd.read_excel('GOOG-NYSE_SPY.xls') 

#set index to the column of your choice, in this case it would be date 
df.set_index('Date', inplace = True) 

#choosing the columns of your choice for further manipulation 
df = df[['Open', 'Close']] 

#divide two colums to get the % change 
df = (df['Open'] - df['Close'])/df['Close'] * 100 


print(df.head()) 
관련 문제