Hello, I’m a newbie. I have a csv file with the SP500 tickers. I created a data frame with it. I would like to add (for example) the price and the dividend besides each ticker (dowloading those new data from yahoo finance).
so far, I have this:
import yfinance as yf
import pandas as pd
import numpy as np
here is an example of how you can potentially set it up:
import pandas as pd
# Define column titles
col_titles = ['TICKER', 'PRICE', 'DIVIDEND']
# Define data to be displayed
data = [[' AMD', 145.25, 1.25],
[' TSLA', 198.23, 2.85],
[' GOOG', 758.12, 1.74],
[' INTL', 59.63, 0.89],
[' MSFT', 36.85, 3.45],
[' IBM', 74.08, 1.61]]
# Create panda dataframe
df = pd.DataFrame(data, columns = col_titles)
# Print the dataframe
print('\n', df)
# Bonus - write info to a file:
csv_path = '/Users/your_pc_name_here/Desktop/test_purposes.csv'
with open(csv_path, 'w', encoding = 'UTF-8') as csv_file:
df.to_csv(csv_file)
What you’ll have to do is read the data from your file and format it according to the data array provided in this example such that there are three columns as per the col_titles list. Once you have it in this format, it should be trivial in displaying it. Because you have many tickers, you will have to implement a for loop iterable to create this array. If as you say you are a newbie, I would read up on iterables and for loop theory and applications. Workout some examples independent of this task so that you get a good grasp before tackling it.
Here is a heads up but you can perform your own search.