我一直在python 3.5中的airpassengers数据集上努力使用此代码
from pandas import read_csv
from pandas import DataFrame
from pandas import Grouper
from matplotlib import pyplot
series = read_csv('AirPassengers.csv', header=None, index_col=0,
parse_dates=True, squeeze=True)
groups = series['1949':'1960'].groupby(Grouper(freq='A'))
years = DataFrame()
pyplot.figure()
i = 1
n_groups = len(groups)
for name, group in groups:
pyplot.subplot((n_groups*100) + 10 + i)
i += 1
pyplot.plot(group)
pyplot.show()
我收到以下错误:
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '1949'
答案 0 :(得分:0)
我假设您使用Kaggle AirPassengers数据集。它具有标题,因此您应省略header=None
。
series = read_csv('AirPassengers.csv', index_col=0, parse_dates=True, squeeze=True)
groups = series['1949':'1960'].groupby(Grouper(freq='A'))
pyplot.figure()
i = 1
n_groups = len(groups)
对于子图:您可以使用subplot(xyz),但请注意,所有整数(x,y,z)都必须小于10,这样表格才能起作用。
for name, group in groups:
pyplot.subplot(n_groups, 1, i)
i += 1
pyplot.plot(group)
pyplot.show()