2014-11-06 61 views
0

我有許多優於其中數據被佈置在片材表如下所示轉換表列出在Excel

Original Table 我想,如下所示各該表的轉換爲另一種類似列表的格式。

Required format

在該表中:

Dir -> Name of the tab 
Year -> Same for entire table 
DOM -> Day of the month 
DOW -> Day of the week. 
Hour -> Column label in original table 
Traffic Count -> Value in the original table 

有接近1000個這種片材。每張表中的數據位於相同的位置。做這個的最好方式是什麼?我應該寫一個VBA腳本還是Excel中有任何東西可以用來讓我的生活更輕鬆?

+1

你應該寫一個腳本VBA的問題。 – pnuts 2014-11-06 23:41:12

+0

Power Query(來自Microsoft的插件)有能力在沒有編碼的情況下執行此操作。 Ken Puls已在他的博客[here]中進行了漫步(http://www.excelguru.ca/blog/2013/11/14/un-pivoting-data-in-power-query/) – guitarthrower 2014-11-06 23:54:39

+0

看起來像簡單的方式@guitarthrower。但不幸的是,我有Office 2013家庭版不支持 – Sanket 2014-11-07 00:11:41

回答

2

我解決了使用python和xlrd模塊

import xlrd 
import numpy as np 
from os import listdir, chdir 
import calendar 
import re 

direction = {'N':0,'S':1,'E':2,'W':3} 
rend = [0, 40, 37, 40, 39, 40, 39, 40, 40, 39, 40, 39, 40] 

# Initialize the matrix to store final result 
aData = np.matrix(np.zeros((0,7))) 

# get all the xlsx files from the directory. 
filenames = [ f for f in listdir('.') if f.endswith('.xlsx') ] 

# for each .xlsx in the current directory 
for file in filenames: 
    # The file names are in the format gdot_39446_yyyy_mm.xlsx 
    # yyyy is the year and mm is the month number with 0 -Jan and 11 - Dec 
    # I extracted the month and year info from the file name 

    ms = re.search('.+_.+_.+_([0-9]+)\.',file,re.M).group(1) 
    month = int(ms) + 1 
    year = int(file[11:15]) 

    # open the workbook 
    workbook = xlrd.open_workbook(file) 

    # the workbook has three sheets. I want information from 
    # sheet2 and sheet3 (indexed by 1 adn 2 resp.) 
    for i in range(1,3): 
     sheet = workbook.sheet_by_index(i) 
     di = sheet.name[-1] 
     data = [[sheet.cell_value(r,c) for c in range(2,26)] for r in range(9,rend[month])] 

     mData = np.matrix(data) 
     mData[np.where(mData=='')] = 0 # some cells are blank. Insert 0 in those cells 
     n,d = mData.shape 
     rows = n * d 

     rData = np.matrix(np.zeros((rows,7))) 
     rData[:,0].fill(direction[di]) 
     rData[:,1].fill(year) 
     rData[:,2].fill(month) 
     for i in range(rows): 
      rData[i,3] = (i/24) + 1 
      rData[i,4] = calendar.weekday(year,month,(i/24) + 1) 
      rData[i,5] = i%24 

     for i in range(n): 
      rData[i*24:((i+1)*24),6] = mData[i,:].T 

     aData = np.vstack((aData,rData)) 

    np.savetxt("alldata.csv",aData, delimiter=',', fmt='%s')  
+1

Bravo!非常好!請接受你的答案。祝你好運。 :) – 2014-11-07 06:11:49

+1

謝謝@Vitalie – Sanket 2014-11-07 08:20:09