【问题标题】:I'm getting the data, trying to iterate the dataframe and add row by row. Trying to fetch stock data (single row) for every company我正在获取数据,尝试迭代数据框并逐行添加。尝试获取每家公司的股票数据(单行)
【发布时间】:2021-08-21 11:00:13
【问题描述】:

我正在尝试迭代数据框并获取数据并逐行添加。尝试获取每家公司的股票数据(单行)

代码如下:

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 符号公司数据在特定日期不存在。如何在那些地方给出空值?

【问题讨论】:

    标签: python pandas dataframe loops for-loop


    【解决方案1】:

    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
    

    【讨论】:

    • 如果公司在那一天没有股票怎么办?如何跳过错误。我正在更新问题中返回的错误
    • 当你将它作为一个列表传递时,它只会给出一个警告。而stack 默认会丢弃它。如果您想保留添加的 NaN 值,请将 .stack(dropna=False) 中的 dropna 设置为 false。
    • 好的,会尝试让你知道
    【解决方案2】:

    在您的 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 才有意义。

    【讨论】:

    • 是的,我正在运行代码。完成后会通知您
    • 它没有显示日期索引... .我有一个问题,如果公司在特定日期没有任何股票价值怎么办?它抛出异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2019-09-20
    • 1970-01-01
    • 2016-07-23
    • 2021-12-16
    相关资源
    最近更新 更多