0
我想簡單地從構建的DataFrame中創建一個csv文件,因此我不必使用互聯網來訪問信息。該行是列表中的代碼:'CIK'北京時間「公司」'部門「產業」Python:將Pandas DataFrame轉換爲CSV
我當前的代碼如下:
def stockStat():
doc = pq('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
for heading in doc(".mw-headline:contains('S&P 500 Component Stocks')").parent("h2"):
rows = pq(heading).next("table tr")
cik = []
ticker = []
coName = []
sector = []
industry = []
for row in rows:
tds = pq(row).find("td")
cik.append(tds.eq(7).text())
ticker.append(tds.eq(0).text())
coName.append(tds.eq(1).text())
sector.append(tds.eq(3).text())
industry.append(tds.eq(4).text())
d = {'CIK':cik, 'Ticker' : ticker, 'Company':coName, 'Sector':sector, 'Industry':industry}
stockData = pd.DataFrame(d)
stockData = stockData.set_index('Ticker')
stockStat()
所以你試過'df.to_csv()'? – EdChum
stockData.to_csv()?我已經嘗試過,但它完成了這個過程,並且csv沒有顯示在我的IDE中 –
是的,你有沒有試過? http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html#pandas.DataFrame.to_csv – EdChum