【发布时间】:2021-04-20 03:09:33
【问题描述】:
我想弄清楚在堆叠数据框后如何填充日期之间的空白。我希望每个股票代码都有各自的日期,而不是每个日期只反映一次。感谢您的帮助。
import pandas as pd
from yahoofinancials import YahooFinancials
positions_held = ['MSFT', 'AAPL', 'AMZN','SPOT']
yahoo_financials = YahooFinancials(positions_held)
data = yahoo_financials.get_historical_price_data(start_date='2020-12-28',
end_date='2021-12-31',
time_interval='daily')
df = pd.DataFrame({
a: {x['formatted_date']: x['adjclose'] for x in data[a]['prices']} for a in positions_held
})
df = df.stack(dropna=True)
df.fillna(method='ffill', inplace=True)
print(df)
【问题讨论】:
标签: python pandas dataframe stack