Data Frame to CSV

I am currently trying to export my data frame into a csv. The basis of the code is to run a list of tickers through yahoo_fin and get options data. The code works, and I retrieve info for every ticker, however, when I assign the stocks a data frame to export into csv form, the csv only opens with the last item on the list. Example:

stocks = ['AAPL', 'AMZN']

for stock in stocks:

    try:
        pd.set_option('display.max_columns', None)
        chain = options.get_options_chain(stock, 'October 15, 2021')
        print(chain['calls'])
    except:
        ValueError

    df = pd.DataFrame(chain['calls'], columns=['Contract Name', 'Last Trade Date', 'Strike', 'Last Price', 'Bid', 'Ask', 'Change', 'Volume', 'Implied Volatility'])
    df.to_csv(r'C:Usersetcetc, index= False, header= True)```

#I am new to python and any help is appreciated! I tried running the data through XlsxWriter and it didn't work either. 

#Thanks!





Answer

Your df.to_csv is in the for loop, so it overwrites the previously generated file in every iteration, but the behaviour here should be to append the data at the end of your existing CSV file instead of overwriting.

To do this, you can try writing the CSV with mode = 'a' as an argument along with your index and name parameters, and you have to handle the header parameter, as in general, you won’t be needing it FOR the append data. You can find a solution for it here

Alternatively, the preferred way would be to save your data into a dataframe from the loop and write the whole dataframe outside the loop in a single write operation, that way you won’t have to worry about handling the header.