0

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?

2
  • 1
    By naming differently the csv file? You're looping and at each loop the previous file is replaced... Commented May 17, 2020 at 20:40
  • 1
    You have two different data frames. The last one is overwriting your first one because you're looping twice over the same file name. Perhaps you meant to save it as different files? jam_df.to_csv('jam_df_{name}.csv'.format(name=ticker))? Commented May 17, 2020 at 20:41

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This can be simplified to full_df = pd.concat([price(ticker) for ticket in ["AAPL", "KO"]]).
0

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.

2 Comments

can I make some type of for loop so they can both go into the csv file?
@Wli's answer does exactly that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.