Python 过滤器不工作,范围出错

Python 过滤器不工作,范围出错,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我试图根据日期范围和列进行筛选,然后呈现数据的图表。不幸的是,当我这样做的时候,我得到了一个错误 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"

我试图根据日期范围和列进行筛选,然后呈现数据的图表。不幸的是,当我这样做的时候,我得到了一个错误

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.

太好了,谢谢你的帮助,我还需要了解括号的用法。很高兴能帮上忙,如果我的回答能帮助你解决问题的话,欢迎接受。这里有很多关于这个主题的有用信息和例子!