简体   繁体   中英

Run function multiple times with different inputs and save values

I am trying to get a DataFrame for each "ticker" in list l by running it through the same function.

import yfinance as yf
import pandas as pd
import csv

def price(ticker):
    company = yf.Ticker(ticker)
    price = company.history(period='max')
    price_df = pd.DataFrame(price)
    price_df.to_csv('test2.csv', index=False)

l = ["AAPL", "KO"]
for ticker in l:
    price(ticker)

How can I make a different DataFrame for each entry in l while using the same function?

import yfinance as yf
import pandas as pd
import csv

def price(ticker):
    company = yf.Ticker(ticker)
    price = company.history(period='max')
    price_df = pd.DataFrame(price)
    return price_df

l = ["AAPL", "KO"]
for ticker in l:
    df = price(ticker)  # do something with each dataframe 

I am not sure if I understood your question correctly, but it sounds just like returning the created dataframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM