Reputation: 306
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?
Upvotes: 0
Views: 1068
Reputation: 1105
You need to export after your loop, after having concatenated your df list
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)#useless
print(jam_df)
full_df = pd.concat(jam_list)
full_df.to_csv('jam_df.csv')
see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html?highlight=concat
Upvotes: 2
Reputation: 697
The stuff inside the for loop ran twice, once for "APPL" and once for "KO". The output paths are the same('jam_df.csv'), so the output for APPL got overwritten.
Upvotes: 0