将从Excel读取的数据组织到Pandas DataFrame

时间:2013-07-17 22:47:01

标签: python excel pandas

我使用此脚本的目标是: 1.从excel文件(> 100,000k行)读取timseries数据以及标题(标签,单位) 2.将数据日期转换为pandas dataFrame的最佳日期时间对象 3.能够使用时间戳引用行和系列标签来引用列

到目前为止,我使用xlrd将excel数据读入列表。制作pandas系列,每个列表和使用时间列表作为索引。组合系列与系列标题,以使python字典。将字典传递给pandas DataFrame。尽管我的努力,df.index似乎设置为列标题,我不知道何时将日期转换为datetime对象。

我刚开始使用python 3天前所以任何建议都会很棒!这是我的代码:

    #Open excel workbook and first sheet
    wb = xlrd.open_workbook("C:\GreenCSV\Calgary\CWater.xlsx")
    sh = wb.sheet_by_index(0)

    #Read rows containing labels and units
    Labels = sh.row_values(1, start_colx=0, end_colx=None)
    Units = sh.row_values(2, start_colx=0, end_colx=None)

    #Initialize list to hold data
    Data = [None] * (sh.ncols)

    #read column by column and store in list
    for colnum in range(sh.ncols):
        Data[colnum] = sh.col_values(colnum, start_rowx=5, end_rowx=None)

    #Delete unecessary rows and columns
    del Labels[3],Labels[0:2], Units[3], Units[0:2], Data[3], Data[0:2]   

    #Create Pandas Series
    s = [None] * (sh.ncols - 4)
    for colnum in range(sh.ncols - 4):
        s[colnum] = Series(Data[colnum+1], index=Data[0])

    #Create Dictionary of Series
    dictionary = {}
    for i in range(sh.ncols-4):
        dictionary[i]= {Labels[i] : s[i]}

    #Pass Dictionary to Pandas DataFrame
    df = pd.DataFrame.from_dict(dictionary)

1 个答案:

答案 0 :(得分:10)

你可以直接在这里使用pandas,我通常喜欢创建一个DataFrames字典(键是表格名称):

In [11]: xl = pd.ExcelFile("C:\GreenCSV\Calgary\CWater.xlsx")

In [12]: xl.sheet_names  # in your example it may be different
Out[12]: [u'Sheet1', u'Sheet2', u'Sheet3']

In [13]: dfs = {sheet: xl.parse(sheet) for sheet in xl.sheet_names}

In [14]: dfs['Sheet1'] # access DataFrame by sheet name

您可以查看提供更多选项的docs on the parse(例如skiprows),这些可以让您通过更多控制来解析单张工作表.​​.....