Python 使用“取消堆叠”(unstack)进行Groubpy打印

Python 使用“取消堆叠”(unstack)进行Groubpy打印,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我有以下代码 df = pd.DataFrame({ 'type':['john','bill','john','bill','bill','bill','bill','john','john'], 'num':[1006,1004,1006,1004,1006,1006,1006,1004,1004], 'date':[2017,2016,2015,2017,2017,2013,2012,2013,2012], 'pos':[0,0,1,4,0,3,3,8,9], 'force':[5,2,7

我有以下代码

df = pd.DataFrame({
'type':['john','bill','john','bill','bill','bill','bill','john','john'],
'num':[1006,1004,1006,1004,1006,1006,1006,1004,1004],
'date':[2017,2016,2015,2017,2017,2013,2012,2013,2012],
'pos':[0,0,1,4,0,3,3,8,9],
'force':[5,2,7,10,6,12,4,7,8]})

fig, ax = plt.subplots()

grp=df.sort_values('date').groupby(['type'])

for name, group in grp :
    print(name)
    print(group)
    group.plot(x='date', y='force', label=name)

plt.show()
所得结果如下:

bill
   type   num  date  pos  force
6  bill  1006  2012    3      4
5  bill  1006  2013    3     12
1  bill  1004  2016    0      2
3  bill  1004  2017    4     10
4  bill  1006  2017    0      6
john
   type   num  date  pos  force
8  john  1004  2012    9      8
7  john  1004  2013    8      7
2  john  1006  2015    1      7
0  john  1006  2017    0      5
[综合机动部队条例草案][1] [img2_Force_john][2]

如何获得4个Fig,每个Fig 2行:

  • 图1账单:
    line1(x=日期,y=强制)
    账单编号(1004)/
    line2(x=date,y=force)
    对于
    num(1006)

  • 图2账单:
    line1(x=date,y=pos)
    num(1004)/
    line2(x=date,y=pos)
    用于
    num(1006)

  • 图3:约翰:
    line1(x=date,y=force)
    num(1004)/
    line2(x=date,y=force)
    对于
    num(1006)

  • 图4:john的
    line1(x=date,y=pos)
    num(1004)
    /
    line2(x=date,y=pos)
    用于
    num(1006)

    • 让我们试试这个:

      df = pd.DataFrame({
      'type':['john','bill','john','bill','bill','bill','bill','john','john'],
      'num':[1006,1004,1006,1004,1006,1006,1006,1004,1004],
      'date':[2017,2016,2015,2017,2017,2013,2012,2013,2012],
      'pos':[0,0,1,4,0,3,3,8,9],
      'force':[5,2,7,10,6,12,4,7,8]})
      
      fig, ax = plt.subplots(2,2)
      axi=iter(ax.flatten())
      
      grp=df.sort_values('date').groupby(['type'])
      for name, group in grp :
      #     print(name)
      #     print(group)
          group.set_index(['date','num'])['force'].unstack().plot(title=name+' - force', ax=next(axi), legend=False)
          group.set_index(['date','num'])['pos'].unstack().plot(title=name+ ' - pos', ax=next(axi), legend=False)
          
      plt.tight_layout()
      plt.legend(loc='upper center', bbox_to_anchor=(0, -.5), ncol=2)
      plt.show()
      
      输出:


      根据以下评论更新: 图表: 让我们试试这个:

      df = pd.DataFrame({
      'type':['john','bill','john','bill','bill','bill','bill','john','john'],
      'num':[1006,1004,1006,1004,1006,1006,1006,1004,1004],
      'date':[2017,2016,2015,2017,2017,2013,2012,2013,2012],
      'pos':[0,0,1,4,0,3,3,8,9],
      'force':[5,2,7,10,6,12,4,7,8]})
      
      fig, ax = plt.subplots(2,2)
      axi=iter(ax.flatten())
      
      grp=df.sort_values('date').groupby(['type'])
      for name, group in grp :
      #     print(name)
      #     print(group)
          group.set_index(['date','num'])['force'].unstack().plot(title=name+' - force', ax=next(axi), legend=False)
          group.set_index(['date','num'])['pos'].unstack().plot(title=name+ ' - pos', ax=next(axi), legend=False)
          
      plt.tight_layout()
      plt.legend(loc='upper center', bbox_to_anchor=(0, -.5), ncol=2)
      plt.show()
      
      输出:


      根据以下评论更新: 图表:

      @ .... 非常感谢你的帮助。 不幸的是,在使用以下代码和大数据绘制两条线之后

      for name, group in grp_new:
          axn= group.set_index(['date', 'num'])['pos'].unstack().plot(title= name+' _pos', legend=False)
      
      绘图看起来像。它们不是连续绘图。我试着画单线,结果没问题。

      @ .... 非常感谢你的帮助。 不幸的是,在使用以下代码和大数据绘制两条线之后

      for name, group in grp_new:
          axn= group.set_index(['date', 'num'])['pos'].unstack().plot(title= name+' _pos', legend=False)
      

      绘图看起来像。它们不是连续的绘图。我试着画单线,结果很好。

      非常感谢您的回答,是否可以将最大/最小值作为线添加到每条线fig@user13931873... 对您希望每个。。。我会注意修改答案。我想画出比尔·福斯的(x=最大力值,y=日期)约翰·福斯的(x=最大力值,y=日期)比尔·福斯的(x=最大力值,y=日期)约翰·福斯的(x=最大力值,y=日期)我真的非常感谢你的帮助,我很感激你,但我指的是两条线的力的最大值。所以我想。。。图1为账单:第1行(x=date,y=force)为num(1004)/第2行(x=date,y=force)为num(1006)/第3行(x=date,y=1004和1004的最大力),应该是一条水平线……。还有一个问题,你能不能在没有子图的情况下绘制它。@user13931873我有点困惑。但我将尝试另一个更新。非常感谢您的回答,是否可以将最大/最小值作为行添加到每个行中fig@user13931873... 对您希望每个。。。我会注意修改答案。我想画出比尔·福斯的(x=最大力值,y=日期)约翰·福斯的(x=最大力值,y=日期)比尔·福斯的(x=最大力值,y=日期)约翰·福斯的(x=最大力值,y=日期)我真的非常感谢你的帮助,我很感激你,但我指的是两条线的力的最大值。所以我想。。。图1为账单:第1行(x=date,y=force)为num(1004)/第2行(x=date,y=force)为num(1006)/第3行(x=date,y=1004和1004的最大力),应该是一条水平线……。还有一个问题,你能不能在没有子图的情况下绘制它。@user13931873我有点困惑。但我将尝试另一个更新。您将如何处理丢失的数据?归零或用最后已知的测量值填充?尝试
      group.set_index(['date',num'])['pos'].unstack().ffill().plot(title=name+''u pos',legend=False)
      ?归零或用最后已知的测量值填充?尝试
      group.set_索引(['date','num'])['pos'].unstack().ffill().plot(title=name+''_pos',legend=False)