多索引 pandas 数据帧的值错误

Value error on multiindex pandas dataframe

我正在使用 here 中的软件包。我想做的是计算每个代码的随机值。 我有以下代码:

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
# import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import numpy as np
import ta


html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', {'class': 'wikitable sortable'})

nifty_symbol =[]

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text
    nifty_symbol.append(nifty_symbols)
tickerss = nifty_symbol

df = web.DataReader(tickerss, 'yahoo')


stoch1 = ta.momentum.StochasticOscillator(high= df['High'], low = df['Low'], close = df['Close'], n=14, fillna=False)

上面的代码有效,但是当我尝试时:

stoch1.stoch()

它产生以下错误:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我该如何解决这个错误?

似乎方法 StochasticOscillator 需要单列时间序列。但是,当您从 web.DataReader 构建数据框时,它会为您提供与代码一样多的列。

所以你只需要迭代代码,并将输出时间序列附加到一个新的数据帧:

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup

import pandas as pd
from pandas_datareader import data as web
import ta

html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', {'class': 'wikitable sortable'})

tickers = []

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text
    tickers.append(nifty_symbols)

print(tickers)

df = web.DataReader(tickers, 'yahoo')

stoch_list = pd.DataFrame(columns = tickers, index = df.index)

for t in tickers:
    stoch1 = ta.momentum.StochasticOscillator(
        high = df['High'][t], 
        low = df['Low'][t], 
        close = df['Close'][t], 
        n = 14, 
        fillna = True
    )
    stoch_list[t] = stoch1.stoch()

print(stoch_list)

输出:

            ADANIPORTS.NS  ASIANPAINT.NS  AXISBANK.NS  BAJAJ-AUTO.NS  BAJFINANCE.NS  BAJAJFINSV.NS  BHARTIARTL.NS  ...   TECHM.NS   TITAN.NS  ULTRACEMCO.NS     UPL.NS    VEDL.NS   WIPRO.NS    ZEEL.NS
Date                                                                                                               ...                                                                                 
2015-06-15      21.909922       9.347667    48.704666      91.231693      77.791892      44.191114      30.140809  ...  53.666585  13.368975      14.346124  11.691838  14.367821  56.043549  91.428571
2015-06-16      72.363725      45.265156    74.247676      91.487233      66.564368      40.100098      55.669462  ...  36.333415  64.983097      88.990210  40.529881  21.072792  52.328376  87.301587
2015-06-17      56.363636      66.856091    53.025895      75.153149      91.088110      50.700195      85.667732  ...  21.000163  91.388957      80.205145  86.905950   3.869011  66.513717  92.325610
2015-06-18      56.363636      86.940831    57.636766      89.124719      94.661550      60.670907      71.193579  ...   5.263327  92.608643      78.613103  60.171097   7.142838  84.943018  70.000000
2015-06-19      11.428615      90.558724    66.282281      89.443078      86.336954      60.956522      88.276724  ...  19.378201  87.341779      81.569343  47.826973  22.928220  64.704798  39.333360
...                   ...            ...          ...            ...            ...            ...            ...  ...        ...        ...            ...        ...        ...        ...        ...
2020-06-08      91.351874      57.958253    87.042001      82.309507      94.710350      94.247354      60.630541  ...  90.171597  89.217165      71.472360  88.654333  75.702800  98.275850  78.462774
2020-06-09      88.169607      59.439045    77.926708      79.988254      86.386837      85.820652      35.190610  ...  79.905597  77.139145      68.528865  83.201404  73.694768  80.155220  70.400641
2020-06-10      89.285678      55.534959    84.539778      62.649461      85.347188      80.135976      38.385475  ...  84.417652  65.700137      64.559014  73.540428  79.718864  76.537583  64.615390
2020-06-11      71.651770      29.268271    72.028606      62.966014      72.695166      75.927493      15.018624  ...  64.094315  53.441767      41.152918  62.931034  59.437760  65.384598  40.940166
2020-06-12      85.508938      35.945931    66.097784      77.878054      87.243356      84.211742      37.430114  ...  45.712787  62.894536      53.112690  54.729316  68.473904  53.365369  17.986311

[1228 rows x 50 columns]