1

好吧,够了。我需要有关此 iteritems() 和 append() 程序的帮助...

在这里,我们有一些啤酒桶和威士忌的时间序列价格数据......

    Beer    Whiskey
Date        
1978-12-29  22.60   86.50
1979-01-02  22.68   86.52
1979-01-03  21.86   87.41
1979-01-04  22.32   87.54
1979-01-05  22.55   87.49
1979-01-08  22.31   87.21
1979-01-09  22.40   87.61
1979-01-10  22.07   87.64
1979-01-11  22.07   88.12
1979-01-12  21.76   88.04

我想做的是从这些数据中创建滚动 5 天的返回值。我一直在使用 iteritems() 函数,并且得到了正确的数字。我不明白的第一部分是为什么这个函数重复输出的次数与 DataFrame 中的列一样多(减去索引)。这是代码和输出...

for value in test.iteritems():
    print(((test - test.shift(5))*100)/test.shift(5))

输出

               Beer        Whiskey
Date                          
1978-12-29       NaN       NaN
1979-01-02       NaN       NaN
1979-01-03       NaN       NaN
1979-01-04       NaN       NaN
1979-01-05       NaN       NaN
1979-01-08 -1.283186  0.820809
1979-01-09 -1.234568  1.259824
1979-01-10  0.960659  0.263128
1979-01-11 -1.120072  0.662554
1979-01-12 -3.503326  0.628643
                Beer        Whiskey
Date                          
1978-12-29       NaN       NaN
1979-01-02       NaN       NaN
1979-01-03       NaN       NaN
1979-01-04       NaN       NaN
1979-01-05       NaN       NaN
1979-01-08 -1.283186  0.820809
1979-01-09 -1.234568  1.259824
1979-01-10  0.960659  0.263128
1979-01-11 -1.120072  0.662554
1979-01-12 -3.503326  0.628643

任何想法为什么重复这个确切的输出?

接下来,我创建一个新的 DataFrame 并要求(非常好!)将此输出附加到数据框中。这是代码...

for value in test.iteritems():
    df.append(((test - test.shift(5))*100)/test.shift(5))

这是我收到的错误...


TypeError                                 Traceback (most recent call last)
<ipython-input-133-006bdc416056> in <module>()
      1 for value in test.iteritems():
----> 2     df.append(((test - test.shift(5))*100)/test.shift(5))

TypeError: append() missing 1 required positional argument: 'other'

我的研究表明,当代码中缺少引用时,会发生这种“其他”类型错误。我尝试了“键,值”的不同组合但无济于事。此外,打印功能似乎没有任何问题。如果您有任何想法,请告诉我。提前致谢

4

1 回答 1

1

pandas.iteritems迭代表单对name, columnseries更准确地说),您可以通过查看此示例来检查

for value in test.iteritems():
    print(value[0])

这输出

Beer
Whiskey

这就是为什么您会看到同一帧的多个输出。您的问题的一个简单解决方案是

returns = 100 * test.diff(5) / test.shift(5)
print(returns)
                Beer   Whiskey
Date                          
1978-12-29       NaN       NaN
1979-01-02       NaN       NaN
1979-01-03       NaN       NaN
1979-01-04       NaN       NaN
1979-01-05       NaN       NaN
1979-01-08 -1.283186  0.820809
1979-01-09 -1.234568  1.259824
1979-01-10  0.960659  0.263128
1979-01-11 -1.120072  0.662554
1979-01-12 -3.503326  0.628643
于 2018-12-13T11:04:09.773 回答