JavaQueen
JavaQueen

Reputation: 1185

Shape of passed values error using pandas for csv file

I'm trying to create a csv file with data by using pandas library. I create the data (numerci values) and the index (the date of the value) like this :

    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])

By doing print len(data) and print len(index) I get the value : 8294 for both. By this code, I create the header which is the first column that contains the same text for all the rows : (meaning same text for any date any value) :

                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]]

I pass the index, data and header to the dataframe like this:

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

Here is the error I get :

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)

Unfortunately my code is very complex, I tried to provied the most important parts. My file should be something like this :

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

Am I missing something in the header?

Upvotes: 0

Views: 170

Answers (1)

Mahesh
Mahesh

Reputation: 141

check the type of 'index' using type(index). I think it is series rather than a list.

Upvotes: 1

Related Questions