1

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&region=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!

1 Answer 1

2

comment your for loop and try this

def get_data(x):
    try:
        r = requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/' + x + '?formatted=true&crumb=8ldhetOu7RJ&lang=en-US&region=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com')
        data = r.json()
        financial_data = data['quoteSummary']['result'][0]['defaultKeyStatistics']
        return financial_data['yield']
    except:
        return 'error'


df['Yield'] = df['Symbol'].apply(lambda x: get_data(x))

if you don't want to use apply you can use indexing which would be faster

for i in df.index:
    x = df.at[i, 'Symbol']
    try:
        r = requests.get(
            'https://query2.finance.yahoo.com/v10/finance/quoteSummary/' + x + '?formatted=true&crumb=8ldhetOu7RJ&lang=en-US&region=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com')
        data = r.json()
        financial_data = data['quoteSummary']['result'][0]['defaultKeyStatistics']
        df.at[i, 'Yield'] = financial_data['yield']
    except Exception as e:
        print(e)

in case of error its Yield column will have Nan value in it

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.