In my code below I have a list of entries that go into a function.
import yfinance as yf
import pandas as pd
import csv
jam_list = []
def price(ticker):
company = yf.Ticker(ticker)
price = company.history(period='max')
price_df = pd.DataFrame(price)
price_df.drop(price_df.columns[[0,1,2,4,5,6]], axis = 1, inplace = True)
price_df['tic'] = (ticker)
return price_df
l = ["AAPL", "KO"]
for ticker in l:
jam = price(ticker)
jam_list.append(jam)
jam_df = pd.DataFrame(jam)
print(jam_df)
jam_df.to_csv('jam_df.csv')
When I print the DataFrame jam_df I get this,
Close scoop
Date
1980-12-12 0.41 AAPL
1980-12-15 0.38 AAPL
... ... ...
2020-05-14 309.54 AAPL
2020-05-15 307.71 AAPL
[9940 rows x 2 columns]
Close scoop
Date
1962-01-02 0.00 KO
1962-01-03 0.00 KO
... ... ...
2020-05-14 43.70 KO
2020-05-15 43.26 KO
[14695 rows x 2 columns]
When I export it to a csv file I will only get the KO part, the second part of the print version. How do I make the csv export both parts AAPL and KO?
jam_df.to_csv('jam_df_{name}.csv'.format(name=ticker))?