,我试图迭代数据,获取数据并逐行添加数据。试图获取每个公司的股票数据(单行)
守则如下:
df = pdr.get_data_yahoo('ABB.NS', start = "2021-6-2", end = "2021-6-3")
df产出如下:
Open High Low Close Adj Close Volume
Date
2021-06-02 1698.0 1717.0 1668.0 1700.55 1700.55 314707类似地,我已经列出了公司名称,并希望获取公司的一行并逐行添加它们.
清单是
sym = symbol[:5]
sym产出如下:
['20MICRONS.NS', '21STCENMGM.NS', '3IINFOTECH.NS', '3MINDIA.NS', '3PLAND.NS']我正在尝试的代码是
for i in sym:
df = pdr.get_data_yahoo(i, start = "2021-6-2", end = "2021-6-3")产出如下:
Open High Low Close Adj Close Volume
Date
2021-06-02 14.05 14.05 13.25 13.5 13.5 3861预期产出如下:
Open High Low Close Adj Close Volume
Date
2021-06-02 14.05 14.05 13.25 13.5 13.5 3861
" Other Other Other Other Other Other
" Other Other Other Other Other Other
" Other Other Other Other Other Other
" Other Other Other Other Other Other
Other are the stock values according to the companies输出仅为单行。我试图获得5行,因为我正在迭代5个公司名称。
如果公司没有特定日期的数据,则返回错误如下
Exception in thread Thread-96:
Traceback (most recent call last):
File "c:\python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "c:\python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\venka\all\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "C:\Users\venka\all\lib\site-packages\fix_yahoo_finance\__init__.py", line 322, in _download_one_threaded
period, interval, prepost)
File "C:\Users\venka\all\lib\site-packages\fix_yahoo_finance\__init__.py", line 333, in _download_one
actions=actions, auto_adjust=auto_adjust)
File "C:\Users\venka\all\lib\site-packages\fix_yahoo_finance\__init__.py", line 246, in history
raise ValueError(self.ticker, err_msg)
ValueError: ('ANMOL.NS', 'No data found, symbol may be delisted')特定日期不存在ANMOL.NS符号公司数据。如何在这些地方给出空值?
发布于 2021-06-03 10:33:23
get_data_yahoo可以将列表作为输入,然后将stack转换为长格式:
sym = ['20MICRONS.NS', '21STCENMGM.NS', '3IINFOTECH.NS', '3MINDIA.NS',
'3PLAND.NS']
df = pdr.get_data_yahoo(sym, start="2021-6-2", end="2021-6-3").stack()df
Attributes Adj Close Close High Low Open Volume
Date Symbols
2021-06-02 20MICRONS.NS 60.700001 60.700001 61.900002 59.500000 59.950001 374552
21STCENMGM.NS 15.650000 15.650000 15.650000 15.250000 15.250000 1810
3IINFOTECH.NS 9.200000 9.200000 9.300000 8.650000 8.850000 39107857
3MINDIA.NS 25967.199219 25967.199219 26000.000000 25543.750000 25640.000000 3698
3PLAND.NS 13.500000 13.500000 14.050000 13.250000 14.050000 3861
2021-06-03 20MICRONS.NS 62.549999 62.549999 64.349998 61.099998 62.250000 401022
21STCENMGM.NS 15.950000 15.950000 15.950000 15.950000 15.950000 949
3IINFOTECH.NS 8.950000 8.950000 9.250000 8.900000 9.200000 17823524
3MINDIA.NS 26261.800781 26261.800781 26300.000000 25900.000000 25967.199219 2713
3PLAND.NS 13.950000 13.950000 14.100000 13.400000 14.000000 19728(可选的reset_index将MultiIndex转换为列)
df = (
pdr.get_data_yahoo(sym, start="2021-6-2", end="2021-6-3")
.stack()
.reset_index()
)df
Attributes Date Symbols Adj Close Close High Low Open Volume
0 2021-06-02 20MICRONS.NS 60.700001 60.700001 61.900002 59.500000 59.950001 374552
1 2021-06-02 21STCENMGM.NS 15.650000 15.650000 15.650000 15.250000 15.250000 1810
2 2021-06-02 3IINFOTECH.NS 9.200000 9.200000 9.300000 8.650000 8.850000 39107857
3 2021-06-02 3MINDIA.NS 25967.199219 25967.199219 26000.000000 25543.750000 25640.000000 3698
4 2021-06-02 3PLAND.NS 13.500000 13.500000 14.050000 13.250000 14.050000 3861
5 2021-06-03 20MICRONS.NS 62.549999 62.549999 64.349998 61.099998 62.250000 401022
6 2021-06-03 21STCENMGM.NS 15.950000 15.950000 15.950000 15.950000 15.950000 949
7 2021-06-03 3IINFOTECH.NS 8.950000 8.950000 9.250000 8.900000 9.200000 17823524
8 2021-06-03 3MINDIA.NS 26261.800781 26261.800781 26300.000000 25900.000000 25967.199219 2713
9 2021-06-03 3PLAND.NS 13.950000 13.950000 14.100000 13.400000 14.000000 19728对顺序读取的显式错误处理:
import pandas as pd
import pandas_datareader as pdr
from pandas_datareader._utils import RemoteDataError
sym = ['20MICRONS.NS', '21STCENMGM.NS', '3IINFOTECH.NS', '3MINDIA.NS',
'3PLAND.NS', 'ANMOL.NS']
dfs = []
for s in sym:
try:
dfs.append(pdr.get_data_yahoo(s, start="2021-6-2", end="2021-6-3"))
except RemoteDataError:
print(f'{s} could not be resolved')
df = pd.concat(dfs)
print(df)输出:
ANMOL.NS could not be resolved
High Low ... Volume Adj Close
Date ...
2021-06-02 61.900002 59.500000 ... 374552 60.700001
2021-06-03 64.349998 61.099998 ... 401022 62.549999
2021-06-02 15.650000 15.250000 ... 1810 15.650000
2021-06-03 15.950000 15.950000 ... 949 15.950000
2021-06-02 9.300000 8.650000 ... 39107857 9.200000
2021-06-03 9.250000 8.900000 ... 17823524 8.950000
2021-06-02 26000.000000 25543.750000 ... 3698 25967.199219
2021-06-03 26300.000000 25900.000000 ... 2713 26261.800781
2021-06-02 14.050000 13.250000 ... 3861 13.500000
2021-06-03 14.100000 13.400000 ... 19728 13.950000发布于 2021-06-03 10:19:45
在for-循环的每一次迭代中,都会覆盖前面的'df‘值。解决办法之一是:
df_list = []
for i in sym:
df_list.append(pdr.get_data_yahoo(i, start = "2021-6-2", end = "2021-6-3"))
df = pd.concat(df_list, axis=0)编辑:我看到你有‘日期’作为df的索引。您将需要玩这个,以便您的最终df是有意义的。
https://stackoverflow.com/questions/67819452
复制相似问题