I have a dataframe df
of stock prices of length ~600k, which I downloaded from here.
我将最后一个列名从“名称”重命名为“ ticks”,并创建了一个新的空白列,名为“名称”:
df = df.rename(columns={'Name': 'Ticker'})
df['Name'] = ''
我编写了以下函数来返回给定股票代码的公司名称:
! pip3 install yfinance
import yfinance as yf
def return_company_name(ticker):
return yf.Ticker(ticker).info['longName']
return_company_name('MSFT')
>>> 'Microsoft Corporation'
现在,我想用相应的股票代码的公司名称填充“名称”列。为此,我编写了以下lambda函数:
df.Name = df.Ticker.apply(lambda x: return_company_name(x))
但是,这最后一行代码仍在继续运行。有什么问题吗?如果是,该如何解决?
I tried the same with map
instead of apply
, but same result.
Looking at the source from yfinance you can see here that the
get_info
method calls_get_fundamentals
which in turn seems to do quite a few API calls to different sites to get the information it needs.由于这是针对每一行执行的,因此您会遇到一些麻烦,因为网站可能会限制您的访问速度。 也许您可以迈出第一步,获取所有唯一名称,然后查找一次,然后将其保存在某种形式的CSV或类似格式的查询中