0

I am trying to pivot a data through DF however i am confused on how to proceed with the below.

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

The above is my raw data and i want my data frame pivot to have the total count of status - column as below individually.

MP Closed Resolved Assigned

However i am unsure how to do it in pandas since pivot just reflects the total count of the status and not each value individually.

  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 
Srini Vasan
  • 83
  • 1
  • 1
  • 7

1 Answers1

0

Use crosstab with remove last row:

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @jerzrael I apologize for the confusion. I wanna know how to do it in pandas just like we do in excel pivoting. The status column has 3 values - Assigned, closed & resolved. When i use the status column in excel it shows us the count of those 3 values. However in pandas i am unable to confirm the same. Please assist. – Srini Vasan Apr 28 '20 at 12:53
  • @SriniVasan - Can you test now? – jezrael Apr 28 '20 at 12:59