熊猫将行值透视到数据框中的列

时间:2020-04-28 11:51:24

标签: python pandas

我正在尝试通过DF传递数据,但是我对如何进行以下操作感到困惑。

case_id    status   quantity    MP
301867534   Closed       72   IN
421064162   Assigned    320   IN
423418908   Assigned    240   DE
301777530   Assigned    252   IN
301903013   Assigned    780   IN
423339220   Assigned    616   US
423503206   Resolved    95    DE
423418756   Resolved    120   US
421868409   Resolved    60    UK
301946232   Resolved    72    IN
422456682   Resolved    77    DE
422743650   Resolved    102   US
422404494   WIP         144   FR
423700455   WIP         160   IN
423560978   WIP         164   DE
423652445   WIP         244   US
423212192   WIP         402   IN
423447545   Closed      112   UK
423610645   Closed      74   DE
423667546   Closed      85   ES

以上是我的原始数据,我希望数据框数据透视表具有状态总数-分别如下所示。

已分配MP已关闭解析

但是我不确定如何在熊猫中进行操作,因为数据透视表仅反映状态的总数,而不是每个值的单独计数。

  MP    Assigned    Closed  Resolved    WIP Grand Total
  DE        1           1      2         1   5
  ES        1           1
  FR                    1                    1
  IN       3            1      1         2   7
  UK       1            1                    2
  US       1            2      1             4 

1 个答案:

答案 0 :(得分:0)

使用crosstab删除最后一行:

df = pd.crosstab(df['MP'], df['status'], margins=True,margins_name='Grand Total').iloc[:-1]
print (df)
status  Assigned  Closed  Resolved  WIP  Grand Total
MP                                                  
DE             1       1         2    1            5
ES             0       1         0    0            1
FR             0       0         0    1            1
IN             3       1         1    2            7
UK             0       1         1    0            2
US             1       0         2    1            4