Python help for yahoo finance! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python help for yahoo finance! (/thread-33164.html) |
Python help for yahoo finance! - Zshanar - Apr-03-2021 Hi, I'm new to this forum & my python skills aren't that much good. I was facing a problem related to the code I thought I could get some help here! This is the code! import yfinance as yf import pandas as PD data = yf.download("ATVI", period="max", group_by='tickers') df = PD.DataFrame(data) with open('D:ATVI.csv', 'a+') as f: df.to_csv(f, header=False) print(df)Now this works just fine when I wanna get one ticker historical quotes in csv format But if I want multiple tickers which I get by adding this! data = yf.download("ATVI AAPL BA", period="max", group_by='tickers') I get AAPL & BA quotes as well But the problem is I wanna save all tickers individually like AAPL.csv BA.csv now I don't wanna write each time different code for different tickers I wanna save files with one code now how would I do that if someone could help me out here I would be really thankful! RE: Python help for yahoo finance! - snippsat - Apr-03-2021 Use Code Tags Can try something this. import yfinance as yf import pandas as PD def yf_func(tickers): data = yf.download(tickers, period="max", group_by="tickers") df = PD.DataFrame(data) return df.to_csv(index=False) def save_data(lst_tic, lst_csv): data = list(zip(lst_tic, lst_csv)) for item in data: with open(f"/content/sample_data/{item[0]}.csv", "w") as f: f.write(item[1]) if __name__ == "__main__": lst_tic = ["ATVI", "AAPL", "BA"] lst_csv = [] for tic in lst_tic: lst_csv.append(yf_func(tic)) save_data(lst_tic, lst_csv) RE: Python help for yahoo finance! - Zshanar - Apr-04-2021 HI, Thanks for your reply I tried but it is not working! It's showing an error! [Program finished]If you could suggest more, Thanks. RE: Python help for yahoo finance! - snippsat - Apr-04-2021 Here is code in Notebook tested and it work. Try copy code from there,error should also be on line 5 not 7. That error means that there can be trouble with data variable.Means that you have some character(non-printing garbage characters) in variable name, function, etc. that's not a letter, number, or underscore. Also try delete that line and write it yourself. RE: Python help for yahoo finance! - Zshanar - Apr-04-2021 HI, My apologies it was my mistake, Anyway I can't thank you enough for the code, one thing though when I use your code the csv file doesn't have date column the code I mentioned earlier also downloads that now how could I get date column if you could suggest anything more! I'm sorry for asking again but you are the only one that replied and I don't have much skills in python so I'm asking! Thanks. RE: Python help for yahoo finance! - snippsat - Apr-04-2021 (Apr-04-2021, 10:24 AM)Zshanar Wrote: one thing though when I use your code the csv file doesn't have date columnChange line 7 to: return df.to_csv()And other way to first do df = df.reset_index() then can have df.to_csv(index=False) The result should ne the same. RE: Python help for yahoo finance! - Zshanar - Apr-04-2021 Thanks man! RE: Python help for yahoo finance! - paulyan - Jul-31-2022 Hi Zshanar, See the code below. import yfinance as yf tickers=['ATVI','AAPL','BA'] df = yf.download(tickers, period='max') df.head() Out[838]: Adj Close Close High \ AAPL ATVI BA AAPL ATVI BA AAPL ATVI BA Date 1962-01-02 NaN NaN 0.190931 NaN NaN 0.823045 NaN NaN 0.837449 1962-01-03 NaN NaN 0.194750 NaN NaN 0.839506 NaN NaN 0.851852 1962-01-04 NaN NaN 0.192840 NaN NaN 0.831276 NaN NaN 0.853909 1962-01-05 NaN NaN 0.189022 NaN NaN 0.814815 NaN NaN 0.835391 1962-01-08 NaN NaN 0.189499 NaN NaN 0.816872 NaN NaN 0.829218 Low Open Volume AAPL ATVI BA AAPL ATVI BA AAPL ATVI BA Date 1962-01-02 NaN NaN 0.823045 NaN NaN 0.837449 NaN NaN 352350 1962-01-03 NaN NaN 0.835391 NaN NaN 0.835391 NaN NaN 710775 1962-01-04 NaN NaN 0.831276 NaN NaN 0.839506 NaN NaN 911250 1962-01-05 NaN NaN 0.792181 NaN NaN 0.831276 NaN NaN 880875 1962-01-08 NaN NaN 0.804527 NaN NaN 0.814815 NaN NaN 473850 |