iteritems()DateFrame输出机制,并将append()输出到新的DataFrame

时间:2018-12-13 10:36:21

标签: python pandas append iteritems

好,足够了。我需要有关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并要求(非常好!)将此输出附加到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'

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

1 个答案:

答案 0 :(得分:1)

pandas.iteritems循环访问name, column形式的对(更精确地说是series),您可以通过查看此示例来检查

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