gpt4 book ai didi

python - Pandas DF 堆叠过滤器不起作用,在范围内出错

转载 作者:行者123 更新时间:2023-12-04 09:30:13 24 4
gpt4 key购买 nike

我正在尝试根据日期范围和列进行过滤,然后显示数据图表。不幸的是,我在这样做时遇到了错误。

import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plot

ticker = yf.Ticker("mnf.ax")
history = ticker.history(period="max").reset_index()
history["Date"] = pd.to_datetime(history["Date"])
history = history.set_index('Date')

# this works
history['2019-01-01' : '2020-07-01'].resample('W').mean().plot.line(figsize=(20, 16), subplots=True)

# This also works
history[['Low','High']].resample('W').mean().plot.line(figsize=(20, 16), subplots=True)

# However this doesn't
history[['2019-01-01' : '2020-07-01'],['Low','High]]'.resample('W').mean().plot.line(figsize=(20, 16), subplots=True)
想要一些见解

最佳答案

您似乎错误地使用了括号。我建议你使用 loc对于这种情况:

history.loc['2019-01-01':'2020-07-01'][['Low','High']].resample('W').mean().plot.line(figsize=(20, 16), subplots=True)
分解:
history.loc['2019-01-01':'2020-07-01'] #Returns the full dataframe within the specified dates

[['Low','High']] # Selects the specific columns

.resample('W').mean().plot.line(figsize=(20, 16), subplots=True) ## Resamples, aggregates using mean and plots.

关于python - Pandas DF 堆叠过滤器不起作用,在范围内出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62877397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com