熊猫 - 绘制累积天数与体积

时间:2017-09-08 23:30:54

标签: python pandas matplotlib dataframe

我尝试使用熊猫绘制音量/天与天数,但我的解决方案输出的数据不超过第一个值(CumTime [0],R [0])。下面是一个初始表如何看起来的示例,并附带它是我想要获得的输出/结果。任何建议/帮助将不胜感激。谢谢

表:



<table style="width:50%">
  <tr>
    <th>ID</th>
    <th>Date</th> 
    <th>Days</th>
    <th>Volume/Day</th>
  </tr>
    <tr>
    <td>a2</td>
    <td>01/01/2014</td>
    <td>20</td>
    <td>60</td>
  </tr>
  <tr>
    <td>a1</td>
    <td>01/01/2014</td>
    <td>15</td>
    <td>100</td>
  </tr>
  <tr>
    <td>a1</td>
    <td>02/01/2014</td>
    <td>30</td>
    <td>80</td>
  </tr>
  <tr>
    <td>a2</td>
    <td>02/01/2014</td>
    <td>20</td>
    <td>40</td>
  </tr>
</table>
&#13;
&#13;
&#13;

尝试解决方案:

df_grp=df.groupby('ID')
for key, grp in df_grp:
def final_result(all_data):
    for key, grp in all_data:
        grp.set_index('Date',inplace=True)
        CumTime = grp['Days'].cumsum()
        R = grp['Volume/Day']
    return CumTime,R  

CumTime,R = final_result(df_grp)

预期结果:

&#13;
&#13;
<table style="width:50%">
  <tr>
    <th>ID</th>
    <th>Date</th>
    <th>Days(***Cumulative_days)</th>
    <th>Volume/Day</th>
  </tr>
  <tr>
    <td>a1</td>
    <td>01/01/2014</td>
    <td>15</td>
    <td>100</td>
  </tr>
  <tr>
    <td>a1</td>
    <td>02/01/2014</td>
    <td>45</td>
    <td>80</td>
  </tr>
 <tr>
    <th>ID</th>
    <th>Date</th>
    <th>Days(***Cumulative_days)</th>
    <th>Volume/Day</th>
  </tr>
  <tr>
    <td>a2</td>
    <td>01/01/2014</td>
    <td>65</td>
    <td>60</td>
  </tr>
  <tr>
    <td>a2</td>
    <td>02/01/2014</td>
    <td>85</td>
    <td>40</td>
  </tr>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

IIUC,您可以按convo->activation->poolingstruct Foo {}; struct Bar { Foo /*const goes here if `const Bar`*/ *foo; }; void f(Bar& bar) { *bar.foo = Foo(); // OK } void g(const Bar& bar) { *bar.foo = Foo(); // OK - but should be error } 排序,然后找到ID

Days

要只显示,您可以拨打cumsum并分别打印每个组。

df = df.sort_values(['ID', 'Days'])
df.Days = df.Days.cumsum()
df

   ID        Date  Days  Volume/Day
1  a1  01/01/2014    15         100
2  a1  02/01/2014    45          80
0  a2  01/01/2014    65          60
3  a2  02/01/2014    85          40

要绘制地图,请使用df.groupby

for k, g in df.groupby("ID"):
     print(g.drop('ID', 1).set_index("Date"))

            Days  Volume/Day
Date                        
01/01/2014    15         100
02/01/2014    45          80

            Days  Volume/Day
Date                        
01/01/2014    65          60
02/01/2014    85          40

enter image description here