Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拆分子批次的groupby结果_Python_Pandas_Matplotlib_Pandas Groupby - Fatal编程技术网

Python 拆分子批次的groupby结果

Python 拆分子批次的groupby结果,python,pandas,matplotlib,pandas-groupby,Python,Pandas,Matplotlib,Pandas Groupby,我有一个数据框,stock\u data.head(): 下面是从数据帧生成groupby命令的代码: avg_permonth = stock_data.set_index('date').groupby(pd.Grouper(freq='M')) avg_permonth['volume'].mean() date 2013-02-28 5.261789e+06 2013-03-31 4.825485e+06 2013-04-30 4.990292e+06 2013-0

我有一个数据框,
stock\u data.head()

下面是从数据帧生成
groupby
命令的代码:

avg_permonth = stock_data.set_index('date').groupby(pd.Grouper(freq='M'))
avg_permonth['volume'].mean()


date
2013-02-28    5.261789e+06
2013-03-31    4.825485e+06
2013-04-30    4.990292e+06
2013-05-31    4.836257e+06
2013-06-30    5.145598e+06
                  ...     
2017-10-31    3.903486e+06
2017-11-30    4.133801e+06
2017-12-31    3.919748e+06
2018-01-31    4.486669e+06
2018-02-28    6.249305e+06
Freq: M, Name: volume, Length: 61, dtype: float64
我的问题是,如何将这些结果按年份分割,然后创建日期与数量的子批次?(我是否必须先重置索引(),或者我是否可以将
索引绘制为
x轴
?)我希望从
2013-02
2018-02
的5个子批次

我尝试了这一点,但没有给出我想要的结果-这是5个子图,但每个图的
x轴
都是从2013年到2018年的,并且都有相同的图


我希望第一个子地块上的x轴从2013年的第一个日期值到2013年的最后一个日期值,第二个地块从2014年开始,依此类推。

您可以这样做:

import matplotlib.pyplot as plt

# dummy values like your serie
s = pd.Series(range(12*6), 
              index=pd.date_range('2013-01-01', '2018-12-31', freq='M'), 
              name='volume')

# Create figure and axs for multiple Axes
fig, axs = plt.subplots(3, 2, figsize=(12,12))

# groupby year to plot each year un a subplot
for i, (year, sg) in enumerate(s.groupby(s.index.year)):
    # plot each year in a subplot
    sg.plot(ax=axs[i//2, i%2]) # here you can add several parameters for colors and stuff
    # or axs[i//2, i%2].plot(sg.index, sg.values) would give the same result
plt.show() 

这与
for
循环的
顶行有问题:
AttributeError:无法访问“DataFrameGroupBy”对象的可调用属性“groupby”,请尝试使用“apply”方法
。作为对
s.groupby(s.index.year)
的交换,我已将我的版本
avg\u permonth.groupby(avg\u permonth.index.year)
。我不能再执行另一个
groupby
,因为它已经完成了@本。T@pragmaticlearner我所称的
s
实际上是与
avg_permonth['volume']相同的结构。mean()
,因为我知道这是你想要绘制的,但是如果你的目标是绘制所有的点,那么你可以用
s.groupby(s.index.year)
by
stock_data.set_index('date')。groupby(pd.Grouper(freq='Y'))['volume']
import matplotlib.pyplot as plt

# dummy values like your serie
s = pd.Series(range(12*6), 
              index=pd.date_range('2013-01-01', '2018-12-31', freq='M'), 
              name='volume')

# Create figure and axs for multiple Axes
fig, axs = plt.subplots(3, 2, figsize=(12,12))

# groupby year to plot each year un a subplot
for i, (year, sg) in enumerate(s.groupby(s.index.year)):
    # plot each year in a subplot
    sg.plot(ax=axs[i//2, i%2]) # here you can add several parameters for colors and stuff
    # or axs[i//2, i%2].plot(sg.index, sg.values) would give the same result
plt.show()