TypeError:__init __()为参数'index'获得了多个值

时间:2018-07-26 17:26:48

标签: python pandas dataframe

我试图在Python中创建一个DataFrame,以便我的索引是日期和时间值,并且有两个对应的列,它们看起来像是:this

我正在使用:

import numpy as np
import pandas as pd 
from pandas import DataFrame 
import datetime



 x = [0.03454225 0.02062136 0.00186715 0.01517354 0.0129046  0.02231125
 0.01492537 0.09646542 0.28444476]

   y = [2.25226244 1.44078451 0.99174488 0.71179491 0.92824542 1.67776948
 2.96399534 5.06257161 7.06504245]

  Date = 2012-01-01 01:00:00 ,2012-01-01 02:00:00, 2012-01-01 03:00:00,2012-01-01 04:00:00,2012-01-01 05:00:00,2012-01-01 06:00:00,2012-01-01 07:00:00,2012-01-01 08:00:00,2012-01-01 09:00:00, 2012-01-01 10:00:00



df = pd.DataFrame(DateTime, x,y ,columns=['Date','X','y'])

print (df )

我的数据形状为:

> x.shape =  (9,) , y.shape = (9,)

但是Date.shape显示错误AttributeError: 'list' object has no attribute 'shape'

将其放入数据框的帮助将受到赞赏

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

import numpy as np
import pandas as pd 
from pandas import DataFrame 
from datetime import datetime, date, time


#added first element as 0 since there was column mismatch with x,y and Date in the code snippet in the question
x = [0, 0.03454225, 0.02062136, 0.00186715, 0.01517354, 0.0129046,  0.02231125, 0.01492537, 0.09646542, 0.28444476]

y = [0, 2.25226244, 1.44078451, 0.99174488, 0.71179491, 0.92824542, 1.67776948, 2.96399534, 5.06257161, 7.06504245]

#pass as string
Date = ['2012-01-01 01:00:00' ,'2012-01-01 02:00:00', '2012-01-01 03:00:00', '2012-01-01 04:00:00', '2012-01-01 05:00:00', '2012-01-01 06:00:00', '2012-01-01 07:00:00', '2012-01-01 08:00:00', '2012-01-01 09:00:00', '2012-01-01 10:00:00']

#convert string to datetime using list comprehension
dates=[datetime.strptime(x,'%Y-%m-%d %H:%M:%S') for x in Date]

#convert lists to dataframes with column names
df = pd.DataFrame({'date':dates,
                   'X':x,
                   'Y':y})

print (df)