合并某些行的时间序列数据。

huangapple go评论102阅读模式
英文:

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:

  1. 什么是合并某些行的时间序列数据的最有效方式
  2. 在我的情况下我有四只不同数据框中的四只股票的时间序列数据
  3. 它们都有一些列但我只关心'Close'
  4. 下面是我想出的方式这是最有效的方式吗pandas和时间序列不熟悉)?
  5. 干杯
  6. def start(verbose=False):
  7. price_tsla = download_normalize_prices('TSLA', 'Tsla')
  8. price_aapl = download_normalize_prices('AAPL', 'Aapl')
  9. price_amzn = download_normalize_prices('AMZN', 'Amzn')
  10. price_sp500 = download_normalize_prices('^GSPC', 'SP500')
  11. combined_df = pd.concat([price_tsla, price_aapl, price_amzn, price_sp500], axis=1)
  12. combined_df.plot()
  13. plot.show()
  14. print(combined_df.head(5))
  15. def download_normalize_prices(ticker, new_col_name):
  16. cols_to_drop = ['Open', 'High', 'Low', 'Volume', 'Dividends', 'Stock Splits']
  17. prices = yf.Ticker(ticker).history(start='2022-1-1', end='2022-12-31')
  18. prices = prices.drop(cols_to_drop, axis=1)
  19. prices = normalize_price(prices)
  20. prices = prices.rename(columns={'Close': new_col_name})
  21. return prices
  22. def normalize_price(price_df):
  23. price_at_row_0 = price_df['Close'].iloc[0]
  24. 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

  1. def start(verbose: False):
  2. price_tsla = download_normalize_prices('TSLA', 'Tsla')
  3. price_aapl = download_normalize_prices('AAPL', 'Aapl')
  4. price_amzn = download_normalize_prices('AMZN', 'Amzn')
  5. price_sp500 = download_normalize_prices('^GSPC', 'SP500')
  6. combined_df = pd.concat([price_tsla, price_aapl, price_amzn, price_sp500], axis=1)
  7. combined_df.plot()
  8. plot.show()
  9. print( combined_df.head(5))
  10. def download_normalize_prices(ticker, new_col_name):
  11. cols_to_drop = ['Open', 'High', 'Low', 'Volume', 'Dividends', 'Stock Splits']
  12. prices = yf.Ticker(ticker).history(start='2022-1-1', end='2022-12-31')
  13. prices = prices.drop(cols_to_drop, axis=1)
  14. prices = normalize_price(prices)
  15. prices = prices.rename(columns={'Close': new_col_name})
  16. return prices
  17. def normalize_price( price_df ):
  18. price_at_row_0 = price_df['Close'].iloc[0]
  19. return price_df.div(price_at_row_0).mul(100)

答案1

得分: 1

以下是翻译好的代码部分:

  1. 这里有一个提议
  2. def start():
  3. d = {"TSLA": "Tsla", "AAPL": "Aapl", "AMZN": "Amzn", "^GSPC": "SP500"}
  4. prices = {ticker: download_prices(ticker) for ticker in d}
  5. combined_df = pd.DataFrame(prices, columns=d)
  6. combined_df.plot()
  7. print(combined_df.head(5))
  8. def download_prices(ticker):
  9. prices = yf.Ticker(ticker).history(
  10. start="2022-01-01", end="2022-12-31")["Close"]
  11. return normalize_prices(prices)
  12. def normalize_prices(prices):
  13. return prices.div(prices.iloc[0]).mul(100)
  14. start()

希望这对你有所帮助!如果你需要更多信息,请随时告诉我。

英文:

Here is a proposition :

  1. def start():
  2. d = {"TSLA": "Tsla", "AAPL": "Aapl", "AMZN": "Amzn", "^GSPC": "SP500"}
  3. prices = {ticker: download_prices(ticker) for ticker in d}
  4. combined_df = pd.DataFrame(prices, columns=d)
  5. combined_df.plot()
  6. print(combined_df.head(5))
  7. def download_prices(ticker):
  8. prices = yf.Ticker(ticker).history(
  9. start="2022-01-01", end="2022-12-31")["Close"]
  10. return normalize_prices(prices)
  11. def normalize_prices(prices):
  12. return prices.div(prices.iloc[0]).mul(100)
  13. start()

Output :

  1. Tsla Aapl Amzn SP500
  2. Date
  3. 2022-01-03 00:00:00-05:00 100.000000 100.000000 100.000000 100.000000
  4. 2022-01-04 00:00:00-05:00 95.816730 98.730844 98.308441 99.937038
  5. 2022-01-05 00:00:00-05:00 90.693293 96.104607 96.451091 97.998983
  6. 2022-01-06 00:00:00-05:00 88.741268 94.500311 95.803809 97.904535
  7. 2022-01-07 00:00:00-05:00 85.595694 94.593707 95.393024 97.508000

合并某些行的时间序列数据。

huangapple
  • 本文由 发表于 2023年6月15日 01:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定