How do I look up a dataframe of S&P 500 stock data, either by the company's name or symbol:
Company Symbol Weight Price Chg % Chg
4 Alphabet Inc. Class A GOOGL 2.173993 2924.72 -8.38 (-0.29%)
Right now, I'm printing that using print(df.loc[[4]])
as in my list Google is index number 4. I was wondering if there exists another loc
command so that if the input is either the symbol or the name, the same thing will be printed. Below I have attached my current code:
# Web-scraped S&P 500 data for 500 US stocks.
import requests
import pandas as pd
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'}
url = 'https://www.slickcharts.com/sp500' # Data from SlickCharts
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
table1 = soup.find('table', attrs={'class':'table table-hover table-borderless table-sm'})
for row in table1.find_all('tr'):
all_td_tags = row.find_all('td')
if len(all_td_tags) > 0:
company = all_td_tags[1].text
symbol = all_td_tags[2].text
weight = all_td_tags[3].text
price = all_td_tags[4].text
chg = all_td_tags[5].text
perChg = all_td_tags[6].text
# print(company, '|', symbol, '|', weight, '|', price, '|', chg, '|', perChg)
df = pd.read_html(str(table1))[0]
df.drop(['#'], axis = 1, inplace = True)
print(df.loc[[4]])
[Context: I'm trying to make a stock simulator game with S&P 500 data, which I have web-scraped from a host site.]
CodePudding user response:
You can find rows by:
df[df['Symbol'] == 'GOOGL']
Output:
CodePudding user response:
Recommend you make the stock symbol the index with df.set_index('Symbol')
. Then you can do either:
Lookup-by-symbol
df.loc['GOOGL']
Lookup-by-company-name
df[df['Company'] == 'Name of Company']
(Probably looking up by Symbol is the common case. That's why you make it the index.)