I tried going through other questions so hopefully this isn't a repetitive question.
I have stock symbols in a csv (stocks.csv) where I'm trying to find the yield. I'm leveraging Pandas, Requests, and Beautiful Soup, but I'm struggling with the Pandas portion. Here's what the csv looks like: CSV output in pandas
Here's my existing code:
import pandas as pd
import requests
from bs4 import BeautifulSoup
# Read csv
df = pd.read_csv(r"C:\Users\User\Desktop\stocks.csv")
for symbol in df['Symbol']:
temp_var = df.loc[df['Symbol'] == symbol, :]
try:
r = requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/' + symbol + '?formatted=true&crumb=8ldhetOu7RJ&lang=en-US®ion=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com')
data = r.json()
financial_data = data['quoteSummary']['result'][0]['defaultKeyStatistics']
yield_dict = financial_data['yield']
df.set_value(df,"Yield", yield_dict['fmt'])
except:
pass
The issue I'm having I believe is I don't know how to create a new column called "Yield" and set the value of the column accordingly. My existing code works fine, but it creates brand new rows with the symbol and yield.
Would appreciate if someone could help me figure out how to create/append an existing pandas column row-by-row in a for loop.
Thank you!