使用pandas for csv file

时间:2017-01-05 11:11:44

标签: python pandas

我尝试使用pandas库创建包含数据的csv文件。我创建数据(numerci值)和索引(值的日期),如下所示:

    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)

不幸的是我的代码非常复杂,我试图提供最重要的部分。 我的文件应该是这样的:

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

我在标题中遗漏了什么?

1 个答案:

答案 0 :(得分:1)

使用type(index)检查'index'的类型。我认为这是系列而不是列表。