8

このスクリプトでの私の目標は次のとおりです。タイムスタンプを使用して行を参照し、系列ラベルを使用して列を参照します

これまでのところ、xlrd を使用して Excel データをリストに読み込みました。各リストでパンダシリーズを作成し、時間リストをインデックスとして使用しました。シリーズとシリーズ ヘッダーを組み合わせて Python 辞書を作成します。pandas DataFrame に辞書を渡しました。私の努力にもかかわらず、 df.index は列ヘッダーに設定されているようで、いつ日付を日時オブジェクトに変換するのかわかりません。

3 日前に Python を使い始めたばかりなので、アドバイスをいただければ幸いです。これが私のコードです:

    #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)
4

1 に答える 1