Iterating through a for loop and saving the retrieved data in a unique csv file for each loop | Python

Viewed 1206

I'm new to python and I'm trying to retrieve stock data using a for loop and then save that information as a dataframe to then save as .csv. Basically, I'm creating a custom_stock_data function that will pass the ticker_list (containing the stock symbols). I then want each item on this list to be passed through the for loop so that the data can be saved as a dataframe in the variable ticker_data and save it as .csv. I then do ticker_data += 1 to move to the next csv file (I don't know if this is how you can do it, any help is welcome!)

This is the code:

def custom_stock_data (ticker):
    ticker_data = []
    for symbol in ticker:
        ticker_data = pd.DataFrame(get_data(symbol, start_date = "05/25/2021", end_date = "07/30/2021", interval = "1wk"))
        ticker_data.to_csv(r"C:\xampp\htdocs\estagio\data_{}.csv".format(symbol))
    ticker_data += 1
    print(ticker_data)

ticker_list = ["AMZN", "TSLA", "AAPL"]
custom_stock_data(ticker_list)

I am also only getting the last information on my ticker_list (which would be AAPL). I can't seem to be getting the information on the first two tickers, as shown in the picture

APPL stock

Thank you!

3 Answers

Take out ticker_data = []. It's useless because you reassign the variable inside the loop.

ticker_data += 1 is not needed. for symbol in ticker: goes to the next symbol automatically each time through the loop.

If you want to see each dataframe, put print(ticker_data) inside the loop.

def custom_stock_data (ticker):
    for symbol in ticker:
        ticker_data = pd.DataFrame(get_data(symbol, start_date = "05/25/2021", end_date = "07/30/2021", interval = "1wk"))
        ticker_data.to_csv(r"C:\xampp\htdocs\estagio\data_{}.csv".format(symbol))
        print(ticker_data)

A bit confusing, I guess that could help you try to debug the issue, print the actual symbol you are working with, and before you save the CSV check the value that is being saved in the column ticker, will not solve but let me know the output:

def custom_stock_data (ticker):
    ticker_data = []
    for symbol in ticker:
        print(f"Working with {symbol}")
        ticker_data = pd.DataFrame(get_data(symbol, start_date = "05/25/2021", end_date = "07/30/2021", interval = "1wk"))
        print(f"Value in CSV is {ticker_data['ticker'].unique()}")            
        ticker_data.to_csv(r"C:\xampp\htdocs\estagio \data_{}.csv".format(symbol))
    ticker_data += 1
    print(ticker_data)

ticker_list = ["AMZN", "TSLA", "AAPL"]
custom_stock_data(ticker_list)

You could just removeticker_data = [], ticker_data += 1 and print(ticker_data). The CSV files will be generated without those 3 lines since there is no need to accumulate the data in a list if you only need separate CSVs.

Also, one possible reason why you're getting only information for AAPL (the last ticker in your list) is because the function get_data() might have AAPL hard-coded in it somewhere.

Related