在简单的DataFrame中接收'index index的多个值'

时间:2012-10-03 14:58:53

标签: python pandas dataframe

我有几个列表正在尝试输入数据框:

    mydates = [0, 6, 15, 21, 30, 37, 45, 53]
    prices = [30.4, 32.5, 31.7, 31.2, 32.7, 34.1, 35.8, 37.8]
    mylist = [6.907894736842111, -2.461538461538464, -1.5772870662460567,4.807692307692319,
    4.2813455657492305, 4.985337243401747, 5.5865921787709505, -3.9682539682539684]

我输入

    df = DataFrame(prices, mylist, index=mydates, columns=['Prices', 'Percentage
    s']) 

并收到此错误:

    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: __init__() got multiple values for keyword argument 'index'

如果我输入此内容,一切正常。

    df = DataFrame(prices, index=mydates, columns=['Prices'])

有人可以告诉我如何解决问题吗?

1 个答案:

答案 0 :(得分:1)

In [604]: d = {'dates' : mydates, 'prices': prices, 'mylist': mylist}

In [605]: df= DataFrame(d)

In [606]: print df
   dates    mylist  prices
0      0  6.907895    30.4
1      6 -2.461538    32.5
2     15 -1.577287    31.7
3     21  4.807692    31.2
4     30  4.281346    32.7
5     37  4.985337    34.1
6     45  5.586592    35.8
7     53 -3.968254    37.8

编辑:

正如Wouter Overmeire指出的那样,您可以使用DataFrame({'prices': prices, 'mylist': mylist}, index=columnanme)按列进行索引。