英文:
Combine time series data for certain rows
问题
The provided code appears to be in Python and deals with time series data using the pandas library. Here's the translated code:
什么是合并某些行的时间序列数据的最有效方式?
在我的情况下,我有四只不同数据框中的四只股票的时间序列数据。
它们都有一些列,但我只关心'Close'列。
下面是我想出的方式,这是最有效的方式吗(对pandas和时间序列不熟悉)?
干杯
def start(verbose=False):
price_tsla = download_normalize_prices('TSLA', 'Tsla')
price_aapl = download_normalize_prices('AAPL', 'Aapl')
price_amzn = download_normalize_prices('AMZN', 'Amzn')
price_sp500 = download_normalize_prices('^GSPC', 'SP500')
combined_df = pd.concat([price_tsla, price_aapl, price_amzn, price_sp500], axis=1)
combined_df.plot()
plot.show()
print(combined_df.head(5))
def download_normalize_prices(ticker, new_col_name):
cols_to_drop = ['Open', 'High', 'Low', 'Volume', 'Dividends', 'Stock Splits']
prices = yf.Ticker(ticker).history(start='2022-1-1', end='2022-12-31')
prices = prices.drop(cols_to_drop, axis=1)
prices = normalize_price(prices)
prices = prices.rename(columns={'Close': new_col_name})
return prices
def normalize_price(price_df):
price_at_row_0 = price_df['Close'].iloc[0]
return price_df.div(price_at_row_0).mul(100)
Is there anything else you would like to know or any specific modifications you need?
英文:
What's the most efficient way to combine time series data for certain rows?
In my case, I have time series data for four stocks in different dfs.
They all have a few columns but I am only interested in the 'Close' column.
Below is what I came up with, is this the most efficient way (new to pandas and time series)?
Cheers
def start(verbose: False):
price_tsla = download_normalize_prices('TSLA', 'Tsla')
price_aapl = download_normalize_prices('AAPL', 'Aapl')
price_amzn = download_normalize_prices('AMZN', 'Amzn')
price_sp500 = download_normalize_prices('^GSPC', 'SP500')
combined_df = pd.concat([price_tsla, price_aapl, price_amzn, price_sp500], axis=1)
combined_df.plot()
plot.show()
print( combined_df.head(5))
def download_normalize_prices(ticker, new_col_name):
cols_to_drop = ['Open', 'High', 'Low', 'Volume', 'Dividends', 'Stock Splits']
prices = yf.Ticker(ticker).history(start='2022-1-1', end='2022-12-31')
prices = prices.drop(cols_to_drop, axis=1)
prices = normalize_price(prices)
prices = prices.rename(columns={'Close': new_col_name})
return prices
def normalize_price( price_df ):
price_at_row_0 = price_df['Close'].iloc[0]
return price_df.div(price_at_row_0).mul(100)
答案1
得分: 1
以下是翻译好的代码部分:
这里有一个提议:
def start():
d = {"TSLA": "Tsla", "AAPL": "Aapl", "AMZN": "Amzn", "^GSPC": "SP500"}
prices = {ticker: download_prices(ticker) for ticker in d}
combined_df = pd.DataFrame(prices, columns=d)
combined_df.plot()
print(combined_df.head(5))
def download_prices(ticker):
prices = yf.Ticker(ticker).history(
start="2022-01-01", end="2022-12-31")["Close"]
return normalize_prices(prices)
def normalize_prices(prices):
return prices.div(prices.iloc[0]).mul(100)
start()
希望这对你有所帮助!如果你需要更多信息,请随时告诉我。
英文:
Here is a proposition :
def start():
d = {"TSLA": "Tsla", "AAPL": "Aapl", "AMZN": "Amzn", "^GSPC": "SP500"}
prices = {ticker: download_prices(ticker) for ticker in d}
combined_df = pd.DataFrame(prices, columns=d)
combined_df.plot()
print(combined_df.head(5))
def download_prices(ticker):
prices = yf.Ticker(ticker).history(
start="2022-01-01", end="2022-12-31")["Close"]
return normalize_prices(prices)
def normalize_prices(prices):
return prices.div(prices.iloc[0]).mul(100)
start()
Output :
Tsla Aapl Amzn SP500
Date
2022-01-03 00:00:00-05:00 100.000000 100.000000 100.000000 100.000000
2022-01-04 00:00:00-05:00 95.816730 98.730844 98.308441 99.937038
2022-01-05 00:00:00-05:00 90.693293 96.104607 96.451091 97.998983
2022-01-06 00:00:00-05:00 88.741268 94.500311 95.803809 97.904535
2022-01-07 00:00:00-05:00 85.595694 94.593707 95.393024 97.508000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论