根据最新的列表和排序日期添加新列

时间:2015-01-27 15:36:09

标签: python pandas

我是使用python编写代码的初学者。

我使用Pandas和他的DataReader编写了这个简单的脚本来从yahoo finance中检索多个股票结果:

import pandas as pd
from pandas.io.data import DataReader
from pandas import DataFrame

symbols_list = ['AAPL', 'TSLA', 'YHOO','GOOG', 'MSFT','GILD']

for ticker in symbols_list: 
r = DataReader(ticker, "yahoo", '2015-01-20')
cell= r[['Open','High','Low','Adj Close','Volume']]

print cell

使用此代码,我获得价格股票,其中包含我在"中指定的日期+其他列; [= ...."如下图所示:

                      Open    High     Low  Adj Close    Volume
         Date                                                   
         2015-01-20  107.84  108.97  106.50     108.72  49899900
         2015-01-21  108.95  111.06  108.27     109.55  48575900
         2015-01-22  110.26  112.47  109.72     112.40  53796400
         2015-01-23  112.30  113.75  111.53     112.98  46464800
         2015-01-26  113.74  114.36  112.80     113.10  55375900
                       Open    High     Low  Adj Close   Volume
         Date                                                  
         2015-01-20  193.87  194.12  187.04     191.93  4489400
         2015-01-21  189.55  198.68  189.51     196.57  4144000
         2015-01-22  197.00  203.24  195.20     201.62  4094100
         2015-01-23  200.29  203.50  198.33     201.29  3438600
         2015-01-26  201.83  208.62  201.05     206.55  3224500

我的问题是:如何在列中包含我在symbol_list中指定的代码? 最后一件事:我怎样才能颠倒日期的顺序?我想让它显示最新的第一个(在我的例子中为2015-01-26)。 下面我给你看一个我想要获得的例子(作为第一列的股票代号和倒置的日期顺序)

  TSLA 2015-01-26 201.83 208.62 201.05 206.55 3224500
  TSLA 2015-01-23 200.29 203.50 198.33 201.29 3438600
  TSLA 2015-01-22 197.00 203.24 195.20 201.62 4094100
  TSLA,2015-01-21 189.55 198.68 189.51 196.57 4144000
  TSLA 2015-01-20 193.87 194.12 187.04 191.93 4489400

  AAPL 2015-01-26 113.74 114.36 112.80 113.10 55375900
  AAPL 2015-01-23 112.30 113.75 111.53 112.98 46464800
  AAPL 2015-01-22 110.26 112.47 109.72 112.40 53796400
  AAPL 2015-01-21 108.95 111.06 108.27 109.55 48575900
  AAPL 2015-01-20 107.84 108.97 106.50 108.72 49899900

我尝试了一些在论坛中创建的东西,但这些都没有给我任何结果。 谢谢大家的考虑,希望有人能帮我解决这两个问题。

2 个答案:

答案 0 :(得分:4)

已完成更新代码

在@EdChum的宝贵帮助下,我已经完成了我的代码,我在这篇文章中的请求已经完全满意了(再次感谢大家的帮助)。 获取多个股票价格,一组列+股票代码以及反转日期(和代码)顺序的最终代码如下所示:

import datetime
import pandas as pd
from pandas import DataFrame
from pandas.io.data import DataReader
symbols_list = ['AAPL', 'TSLA', 'YHOO','GOOG', 'MSFT','ALTR','WDC','KLAC']

symbols=[]
for ticker in symbols_list: 
    r = DataReader(ticker, "yahoo", 
                   start=datetime.datetime(2014, 12, 30))
    # add a symbol column
    r['Symbol'] = ticker 
    symbols.append(r)
# concatenate all the dfs
df = pd.concat(symbols)
#define cell with the columns that i need
cell= df[['Symbol','Open','High','Low','Adj Close','Volume']]
#changing sort of Symbol (ascending) and Date(descending) setting Symbol as first column and changing date format
cell.reset_index().sort(['Symbol', 'Date'], ascending=[1,0]).set_index('Symbol').to_csv('stock.csv', date_format='%d/%m/%Y')

希望此代码能够帮助其他用户。

答案 1 :(得分:1)

我想我理解你想要什么,我会将每个df附加到列表中并使用concat制作单个df,对于每个df,我们只需为每个符号添加一个新列:

In [54]:

from pandas.io.data import DataReader
symbols_list = ['AAPL', 'TSLA', 'YHOO','GOOG', 'MSFT','GILD']

symbols=[]
for ticker in symbols_list: 
    r = DataReader(ticker, "yahoo", '2015-01-20')
    # add a symbol column
    r['Symbol'] = ticker
    symbols.append(r)
# concatenate all the dfs
df = pd.concat(symbols)
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 30 entries, 2015-01-20 00:00:00 to 2015-01-26 00:00:00
Data columns (total 7 columns):
Open         30 non-null float64
High         30 non-null float64
Low          30 non-null float64
Close        30 non-null float64
Volume       30 non-null int64
Adj Close    30 non-null float64
Symbol       30 non-null object
dtypes: float64(5), int64(1), object(1)
memory usage: 1.9+ KB
In [55]:

df
Out[55]:
              Open    High     Low   Close    Volume  Adj Close Symbol
Date                                                                  
2015-01-20  107.84  108.97  106.50  108.72  49899900     108.72   AAPL
2015-01-21  108.95  111.06  108.27  109.55  48575900     109.55   AAPL
2015-01-22  110.26  112.47  109.72  112.40  53796400     112.40   AAPL
2015-01-23  112.30  113.75  111.53  112.98  46464800     112.98   AAPL
2015-01-26  113.74  114.36  112.80  113.10  55375900     113.10   AAPL
2015-01-20  193.87  194.12  187.04  191.93   4503200     191.93   TSLA
2015-01-21  189.55  198.68  189.51  196.57   4153000     196.57   TSLA
2015-01-22  197.00  203.24  195.20  201.62   4094100     201.62   TSLA
2015-01-23  200.29  203.50  198.33  201.29   3438600     201.29   TSLA
2015-01-26  201.83  208.62  201.05  206.55   3224500     206.55   TSLA
2015-01-20   46.79   47.89   46.77   47.63  15845900      47.63   YHOO
2015-01-21   47.60   48.38   47.32   48.18  16305100      48.18   YHOO
2015-01-22   48.43   49.08   48.01   48.89  12647400      48.89   YHOO
2015-01-23   48.74   49.23   48.63   48.95  14206100      48.95   YHOO
2015-01-26   49.57   49.79   49.07   49.44  18928700      49.44   YHOO
2015-01-20  511.00  512.50  506.02  506.90   2221800     506.90   GOOG
2015-01-21  507.25  519.28  506.20  518.04   2262500     518.04   GOOG
2015-01-22  521.48  536.33  519.70  534.39   2653600     534.39   GOOG
2015-01-23  535.59  542.17  533.00  539.95   2266800     539.95   GOOG
2015-01-26  538.53  539.00  529.67  535.21   1532400     535.21   GOOG
2015-01-20   46.30   46.65   45.57   46.39  36161900      46.39   MSFT
2015-01-21   45.94   46.14   45.48   45.92  39081100      45.92   MSFT
2015-01-22   46.38   47.14   46.08   47.13  35898000      47.13   MSFT
2015-01-23   47.36   47.39   46.80   47.18  26211600      47.18   MSFT
2015-01-26   47.00   47.13   46.24   47.01  39577800      47.01   MSFT
2015-01-20  101.14  104.05  100.15  103.78  18838100     103.78   GILD
2015-01-21  103.04  105.07  102.57  103.58  12312500     103.58   GILD
2015-01-22  104.14  105.18  101.56  105.14  12963100     105.14   GILD
2015-01-23  104.40  106.11  103.80  105.54   9566700     105.54   GILD
2015-01-26  104.87  107.77  104.87  107.11  10254600     107.11   GILD

要求致电sort并通过ascending=False

In [56]:

df.sort(ascending=False)
Out[56]:
              Open    High     Low   Close    Volume  Adj Close Symbol
Date                                                                  
2015-01-26  104.87  107.77  104.87  107.11  10254600     107.11   GILD
2015-01-26  201.83  208.62  201.05  206.55   3224500     206.55   TSLA
2015-01-26  538.53  539.00  529.67  535.21   1532400     535.21   GOOG
2015-01-26   49.57   49.79   49.07   49.44  18928700      49.44   YHOO
2015-01-26   47.00   47.13   46.24   47.01  39577800      47.01   MSFT
2015-01-26  113.74  114.36  112.80  113.10  55375900     113.10   AAPL
2015-01-23  104.40  106.11  103.80  105.54   9566700     105.54   GILD
2015-01-23   47.36   47.39   46.80   47.18  26211600      47.18   MSFT
2015-01-23  200.29  203.50  198.33  201.29   3438600     201.29   TSLA
2015-01-23  535.59  542.17  533.00  539.95   2266800     539.95   GOOG
2015-01-23   48.74   49.23   48.63   48.95  14206100      48.95   YHOO
2015-01-23  112.30  113.75  111.53  112.98  46464800     112.98   AAPL
2015-01-22  521.48  536.33  519.70  534.39   2653600     534.39   GOOG
2015-01-22   46.38   47.14   46.08   47.13  35898000      47.13   MSFT
2015-01-22  110.26  112.47  109.72  112.40  53796400     112.40   AAPL
2015-01-22   48.43   49.08   48.01   48.89  12647400      48.89   YHOO
2015-01-22  104.14  105.18  101.56  105.14  12963100     105.14   GILD
2015-01-22  197.00  203.24  195.20  201.62   4094100     201.62   TSLA
2015-01-21  507.25  519.28  506.20  518.04   2262500     518.04   GOOG
2015-01-21  189.55  198.68  189.51  196.57   4153000     196.57   TSLA
2015-01-21   47.60   48.38   47.32   48.18  16305100      48.18   YHOO
2015-01-21  103.04  105.07  102.57  103.58  12312500     103.58   GILD
2015-01-21   45.94   46.14   45.48   45.92  39081100      45.92   MSFT
2015-01-21  108.95  111.06  108.27  109.55  48575900     109.55   AAPL
2015-01-20  101.14  104.05  100.15  103.78  18838100     103.78   GILD
2015-01-20  107.84  108.97  106.50  108.72  49899900     108.72   AAPL
2015-01-20   46.30   46.65   45.57   46.39  36161900      46.39   MSFT
2015-01-20   46.79   47.89   46.77   47.63  15845900      47.63   YHOO
2015-01-20  193.87  194.12  187.04  191.93   4503200     191.93   TSLA
2015-01-20  511.00  512.50  506.02  506.90   2221800     506.90   GOOG

<强>更新

如果要保留股票代码分组,然后按日期排序,则需要以下内容。在这里,我必须重置索引,以便'Date'再次成为常规列,然后传递一个列表,它将按顺序处理,最后我必须再次设置索引:

In [44]:

df.reset_index().sort(['Symbol', 'Date'], ascending=False).set_index('Date')
Out[44]:
              Open    High     Low   Close     Volume  Adj Close Symbol
Date                                                                   
2015-01-27   49.14   49.28   47.66   47.99   44813600      47.99   YHOO
2015-01-26   49.57   49.79   49.07   49.44   18928700      49.44   YHOO
2015-01-23   48.74   49.23   48.63   48.95   14206100      48.95   YHOO
2015-01-22   48.43   49.08   48.01   48.89   12647400      48.89   YHOO
2015-01-21   47.60   48.38   47.32   48.18   16305100      48.18   YHOO
2015-01-20   46.79   47.89   46.77   47.63   15845900      47.63   YHOO
2015-01-27  204.42  208.03  203.30  205.98    2777700     205.98   TSLA
2015-01-26  201.83  208.62  201.05  206.55    3224500     206.55   TSLA
2015-01-23  200.29  203.50  198.33  201.29    3438600     201.29   TSLA
2015-01-22  197.00  203.24  195.20  201.62    4094100     201.62   TSLA
2015-01-21  189.55  198.68  189.51  196.57    4153000     196.57   TSLA
2015-01-20  193.87  194.12  187.04  191.93    4503200     191.93   TSLA
2015-01-27   42.95   43.20   42.11   42.66  169112700      42.66   MSFT
2015-01-26   47.00   47.13   46.24   47.01   42525500      47.01   MSFT
2015-01-23   47.36   47.39   46.80   47.18   26211600      47.18   MSFT
2015-01-22   46.38   47.14   46.08   47.13   35898000      47.13   MSFT
2015-01-21   45.94   46.14   45.48   45.92   39081100      45.92   MSFT
2015-01-20   46.30   46.65   45.57   46.39   36161900      46.39   MSFT
2015-01-27  529.97  530.70  518.19  518.63    1897300     518.63   GOOG
2015-01-26  538.53  539.00  529.67  535.21    1532400     535.21   GOOG
2015-01-23  535.59  542.17  533.00  539.95    2266800     539.95   GOOG
2015-01-22  521.48  536.33  519.70  534.39    2653600     534.39   GOOG
2015-01-21  507.25  519.28  506.20  518.04    2262500     518.04   GOOG
2015-01-20  511.00  512.50  506.02  506.90    2221800     506.90   GOOG
2015-01-27  105.94  107.02  104.86  105.03   10334500     105.03   GILD
2015-01-26  104.87  107.77  104.87  107.11   10254600     107.11   GILD
2015-01-23  104.40  106.11  103.80  105.54    9566700     105.54   GILD
2015-01-22  104.14  105.18  101.56  105.14   12963100     105.14   GILD
2015-01-21  103.04  105.07  102.57  103.58   12312500     103.58   GILD
2015-01-20  101.14  104.05  100.15  103.78   18838100     103.78   GILD
2015-01-27  112.42  112.48  109.03  109.14   91929200     109.14   AAPL
2015-01-26  113.74  114.36  112.80  113.10   55615000     113.10   AAPL
2015-01-23  112.30  113.75  111.53  112.98   46464800     112.98   AAPL
2015-01-22  110.26  112.47  109.72  112.40   53796400     112.40   AAPL
2015-01-21  108.95  111.06  108.27  109.55   48575900     109.55   AAPL
2015-01-20  107.84  108.97  106.50  108.72   49899900     108.72   AAPL