简体   繁体   English

类型错误:__init__() 为参数“index”获得了多个值

[英]TypeError: __init__() got multiple values for argument 'index'

I am trying to create a DataFrame in Python so that my index is date and time values and I have two corresponding columns which simply look like:我正在尝试在 Python 中创建一个 DataFrame 以便我的索引是日期和时间值,并且我有两个相应的列,它们看起来像: 这个

I am using:我在用:

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 )

The shape of my data is:我的数据的形状是:

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

but Date.shape shows an error AttributeError: 'list' object has no attribute 'shape'Date.shape显示错误AttributeError: 'list' object has no attribute 'shape'

Help with putting this into a data frame will be appreciated将其放入数据框中的帮助将不胜感激

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:__init __()为参数'n_splits'获取了多个值 - TypeError: __init__() got multiple values for argument 'n_splits' TypeError:__init __()为参数'fieldnames'获得了多个值 - TypeError: __init__() got multiple values for argument 'fieldnames' 类型错误:__init__() 为参数“strides”获得了多个值 - TypeError: __init__() got multiple values for argument 'strides' TypeError:__init__() 为参数“轴”获取了多个值 - TypeError: __init__() got multiple values for argument 'axes' TypeError:__ init __()得到关键字参数'customer'的多个值 - TypeError: __init__() got multiple values for keyword argument 'customer' SQLAlchemy TypeError:__init __()为参数'name'获得了多个值 - SQLAlchemy TypeError: __init__() got multiple values for argument 'name' TypeError:__init__() 为参数“数据”获取了多个值 - TypeError: __init__() got multiple values for argument 'data' TypeError:__init__() 为参数“标记”获取了多个值 - TypeError: __init__() got multiple values for argument 'marks' Python TypeError:__ init __()为参数'master'获取了多个值 - Python TypeError: __init__() got multiple values for argument 'master' TypeError:__init __()为关键字参数“ choices”获得了多个值 - TypeError: __init__() got multiple values for keyword argument 'choices'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM