无法将csv文件处理为带有代码的pandas,并从DataFrame中获取stock-info

时间:2015-02-09 09:29:52

标签: csv python-3.x pandas stock ticker

我正在尝试导入股票代码列表(#symbols_list ... read_csv ..这一行),并将该日期的股票信息提取到熊猫中。

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

#symbols_list = [pd.read_csv('Stock List.csv', index_col=0)]
symbols_list = ['AAPL', 'TSLA', 'YHOO','GOOG', 'MSFT','ALTR','WDC','KLAC']
symbols=[]

start = datetime.datetime(2014, 2, 9)
#end = datetime.datetime(2014, 12, 30)

for ticker in symbols_list:
    r = DataReader(ticker, "yahoo",
                   start = start)
                   #start=start, end)
    # 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')

输入文件stock list.csv 每个单独的行上都有以下内容:

Index
MMM
ABT
ABBV
ACE
ACN
ACT
ADBE
ADT
AES
AET
AFL
AMG

以及更多感兴趣的代码。

使用手动编码列表

运行时
symbols_list = ['AAPL', 'TSLA', 'YHOO','GOOG', 'MSFT','ALTR','WDC','KLAC']

一切正常并处理输入并将其存储到文件中, 但每当我使用read_csv从文件运行代码时,我都会收到以下错误:

runfile('Z:/python/CrystallBall/SpyderProject/getstocks3.py', wdir='Z:/python/CrystallBall/SpyderProject') Reloaded modules: pandas.io.data, pandas.tseries.common Traceback (most recent call last):

  File "<ipython-input-32-67cbdd367f48>", line 1, in <module>
    runfile('Z:/python/CrystallBall/SpyderProject/getstocks3.py', wdir='Z:/python/CrystallBall/SpyderProject')

  File "C:\Program Files (x86)\WinPython-32bit-3.4.2.4\python-3.4.2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)

  File "C:\Program Files (x86)\WinPython-32bit-3.4.2.4\python-3.4.2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 80, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "Z:/python/CrystallBall/SpyderProject/getstocks3.py", line 35, in <module>
    cell.reset_index().sort(['Symbol', 'Date'], ascending=[1,0]).set_index('Symbol').to_csv('stock.csv', date_format='%d/%m/%Y')

  File "C:\Users\Morten\AppData\Roaming\Python\Python34\site-packages\pandas\core\generic.py", line 1947, in __getattr__
    (type(self).__name__, name))

AttributeError: 'Panel' object has no attribute 'reset_index'

为什么我只能处理手动布局的symbol_list,而不能处理来自文件的导入的代码?

任何接受者?任何帮助非常感谢!

1 个答案:

答案 0 :(得分:0)

您的代码存在许多问题,以下代码已修复并运行:

In [4]:

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

temp='''Index
MMM
ABT
ABBV
ACE
ACN
ACT
ADBE
ADT
AES
AET
AFL
AMG'''

df = pd.read_csv(io.StringIO(temp), index_col=[0])
symbols=[]

start = datetime.datetime(2014, 2, 9)

for ticker in df.index:
    r = DataReader(ticker, "yahoo",
                   start = start)
                   #start=start, end)
    # 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')
cell
Out[4]:
           Symbol    Open    High     Low  Adj Close   Volume
Date                                                         
2014-02-10    MMM  129.65  130.41  129.02     126.63  3317400
2014-02-11    MMM  129.70  131.49  129.65     127.88  2604000
...           ...     ...     ...     ...        ...      ...
2015-02-06    AMG  214.35  215.82  212.64     214.45   424400

[3012 rows x 6 columns]

首先这是:symbols_list = [pd.read_csv('Stock List.csv', index_col=0)]

这将创建一个包含单个条目的列表,该列表将是一个没有列的df,只是您的股票价值的索引。

这:for ticker in symbols_list:

将无法工作,因为从df返回的可迭代对象是列而不是每个条目,在您的情况下,您需要迭代索引,这是我的代码所做的。

我不确定你想要实现什么,没有必要指定index_col=0如果只有一列,你可以创建一个只有一列的df,或者你通过squeeze=True这将创建一个只有一列的系列。