0

我想同时阅读几个股票行情的报价。我在用:

import numpy as np
import pandas as pd
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
%matplotlib inline
start = datetime.datetime(2017, 9, 20)
end = datetime.datetime(2017,9,22)
h = web.DataReader(["EWI", "EWG"], "yahoo", start, end)

......它似乎工作。然而,数据被读入面板数据结构。如果我打印变量“h”,我会得到:

<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 4 (major_axis) x 2 (minor_axis)
Items axis: Adj Close to Volume
Major_axis axis: 2017-09-22 00:00:00 to 2017-09-19 00:00:00
Minor_axis axis: EWG to EWI

我想要:

  1. “查看”生成的面板值(我对熊猫比较陌生)。
  2. 是否可以将面板展平为 DataFrame?(IMO 记录得更好)
  3. 如果我为我阅读“调整后的收盘价”就绰绰有余了。也许直接读入 DataFrame 会更容易?

谢谢

4

1 回答 1

1

我认为您需要Panel.to_frameMultiIndex DataFrame

#with random data

df = h.to_frame()
print (df)
                  Adj Close     Close      High       Low      Open    Volume
major      minor                                                             
2013-01-01 EWI     0.471435  0.471435  0.471435  0.471435  0.471435  0.471435
           EWG    -1.190976 -1.190976 -1.190976 -1.190976 -1.190976 -1.190976
2013-01-02 EWI     1.432707  1.432707  1.432707  1.432707  1.432707  1.432707
           EWG    -0.312652 -0.312652 -0.312652 -0.312652 -0.312652 -0.312652
2013-01-03 EWI    -0.720589 -0.720589 -0.720589 -0.720589 -0.720589 -0.720589
           EWG     0.887163  0.887163  0.887163  0.887163  0.887163  0.887163
2013-01-04 EWI     0.859588  0.859588  0.859588  0.859588  0.859588  0.859588
           EWG    -0.636524 -0.636524 -0.636524 -0.636524 -0.636524 -0.636524

然后选择列:

s = df['Adj Close']
print (s)
major       minor
2013-01-01  EWI      0.471435
            EWG     -1.190976
2013-01-02  EWI      1.432707
            EWG     -0.312652
2013-01-03  EWI     -0.720589
            EWG      0.887163
2013-01-04  EWI      0.859588
            EWG     -0.636524
Name: Adj Close, dtype: float64

df1 = df[['Adj Close']]
print (df1)
                  Adj Close
major      minor           
2013-01-01 EWI     0.471435
           EWG    -1.190976
2013-01-02 EWI     1.432707
           EWG    -0.312652
2013-01-03 EWI    -0.720589
           EWG     0.887163
2013-01-04 EWI     0.859588
           EWG    -0.636524

注意:

将来Panel会被弃用

于 2017-09-23T18:35:14.963 回答