如何解决如何将符号添加到多个股票数据
#i我的代码下面是报废的数据,现在我想在相应的公司数据中添加一列符号,请指导我如何将符号添加到相应的公司数据中
下面的#code
from time import sleep
import pandas as pd
import os
import numpy as np
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
browser = webdriver.Chrome(ChromeDriverManager().install())
symbols =['FATIMA','SSGC','FCCL','ISL','KEL','NCL','DGKC','SNGP','NML','ENGRO','HUMNL','CHCC','ATRL','HUBC','ASTL','PIBTL','OGDC','EFERT','FFC','NCPL','KTML','PSO','LUCK','SEARL','KOHC','ABOT','AICL','HASCOL','PTC','KAPCO','PIOC','POL','SHEL','GHGL','HCAR','DCR','BWCL','MTL','GLAXO','PKGS','SHFA','MARI','ICI','ACPL','PSMC','SPWL','THALL','BNWM','EFUG','GADT','AABS']
company = 1
for ThisSymbol in symbols :
# Get first symbol from the above python list
company = 2
# In the URL,make symbol as variable
url = 'http://www.scstrade.com/stockscreening/SS_CompanySnapShotYF.aspx?symbol=' + ThisSymbol
browser.get(url)
sleep(2)
# The below command will get all the contents from the url
html = browser.execute_script("return document.documentElement.outerHTML")
# So we will supply the contents to beautiful soup and we tell to consider this text as a html,with the following command
soup = BeautifulSoup (html,"html.parser")
for rn in range(0,9) :
plist = []
r = soup.find_all('tr')[rn]
# Condition: if first row,then th,otherwise td
if (rn==0) :
celltag = 'th'
else :
celltag = 'td'
# Now use the celltag instead of using fixed td or th
col = r.find_all(celltag)
print()
if col[i] == 0:
print ("")
else:
for i in range(0,4) :
cell = col[i].text
clean = cell.replace('\xa0 ','')
clean = clean.replace (' ','')
plist.append(clean)
# If first row,create df,otherwise add to it
if (rn == 0) :
df = pd.DataFrame(plist)
else :
df2 = pd.DataFrame(plist)
colname = 'y' + str(2019-rn)
df[colname] = df2
if (company == 1):
dft = df.T
# Get header Column
head = dft.iloc[0]
# Exclude first row from the data
dft = dft[1:]
dft.columns = head
dft = dft.reset_index()
# Assign Headers
dft = dft.drop(['index'],axis = 'columns')
else:
dft2 = df.T
# Get header Column
head = dft2.iloc[0]
# Exclude first row from the data
dft2 = dft2[1:]
dft2.columns = head
dft2 = dft2.reset_index()
# Assign Headers
dft2 = dft2.drop(['index'],axis = 'columns')
dft['Symbol'] = ThisSymbol
dft = dft.append(dft2,sort=['Year','Symbol'])
company = company +1
dft
解决方法
我的输出看起来像这样,我希望每个公司数据都有一个符号列 符号,我已添加
dft ['Symbol'] = ThisSymbol
但是它仅将列表中的第一家公司添加到所有公司数据中
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。