Python存储类列表并同时导出两者

对于下面的班级,我试图通过将两只股票的价格数据放入将由班级运行的列表中来获取价格数据。

import yfinance as yf
import pandas as pd
import csv

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

l = ['AAPL', 'KO']
for item in l:
    price(item)

when I export or print price_df I will only get the last entry in my list, i.e. only get "KO" price data without "AAPL" as well. The goal is to have "AAPL" price data directly followed by "KO" in one DataFrame. How would I do this?

评论