2017-01-05 41 views
0

我想通過使用熊貓庫創建一個帶有數據的csv文件。我創建的數據(numerci值)和索引(價值日期)是這樣的:傳遞值使用熊貓csv文件的錯誤形狀

date = chaine[:10] + " " + chaine[11:] 
    date = parseDate(date) 
    i = str(date).replace('-','') 
    i = str(i).replace(':','') 
    i = str(i).replace(' ','') 
    index.append(date) 
    data.append(row[2]) 

這樣做print len(data)print len(index)我得到的值:8294兩個。 通過該代碼,我創建頭,其是包含相同的文字的所有的行的第一列:(意思相同的文字爲任何日期的任何值):

   reader = csv.reader(file) 
       firstline = next(reader) 
       sensorname = firstline[0] 
       secondline = next(reader) 
       colname = sensorname+secondline[2].replace("D1a","") 
       header = [colname for row in secondline[2]] 

我通過索引,數據和頭以數據幀是這樣的:

import pandas as pd 
     newDataframe = pd.DataFrame(data, index=index, columns=header) 

這裏是我的錯誤:

ERROR :: Shape of passed values is (1, 8294), indices imply (2, 8294) 
    newDataframe = pd.DataFrame(data, index=index, columns=header) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 279, in __init__ 
    copy=copy) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 432, in _init_ndarray 
    return create_block_manager_from_blocks([values], [columns, index]) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3993, in create_block_manager_from_blocks 
    construction_error(tot_items, blocks[0].shape[1:], axes, e) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3970, in construction_error 
    passed, implied)) 
ValueError: Shape of passed values is (1, 8294), indices imply (2, 8294) 

不幸的是我的代碼是非常複雜的,我試圖provied最重要的部分。 我的文件應該是這樣的:

"measure:pressure","20161203070000","34.243" 
"measure:pressure","20161204070000","3.53" 
"measure:pressure","20160403070000","77.1" 

我失去了在頭的東西嗎?

+0

請問這個問題和答案[這裏](http://stackoverflow.com/questions/19630265/valueerror-shape-of-passed-values-is-3-27-indices-imply-4-27-pandas- da)有幫助嗎? – gincard

+0

您正在編寫或閱讀csv文件嗎? –

+0

也許你想檢查http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html和http://pandas.pydata.org/pandas-docs/stable/generated/pandas。 DataFrame.to_csv.html –

回答

1

使用類型(索引)檢查'索引'的類型。我認爲這是一系列而不是一個列表。

+1

剛剛檢查@Mahesh和索引是一個列表。 – JavaQueen