当某个符号的"marketCap"
列中没有数据时,我得到的是KeyError: "['marketCap'] not in index"
。当没有数据时,我如何设置“Null”,这样代码就可以继续运行而不会出错?
import pandas as pd
from yahooquery import Ticker
symbols = ['MSFT','GOOG','AAPL'] #I have to put 75,000+ symbols here.
header = ["regularMarketPrice", "marketCap"]
for tick in symbols:
faang = Ticker(tick)
faang.price
df = pd.DataFrame(faang.price).T
df.to_csv('output.csv', mode='a', index=True, header=False, columns=header)
1条答案
按热度按时间hmmo2u0o1#
这是个好主意: