Pyd
Pyd

Reputation: 6159

How to pivot_table with with duplicated index

I have a df_ like this,

name  level  status
yes   high   open
no    high   closed
no    med    closed
yes   low    open
no    med    rejected
no    high   open

I am trying to create a pivot table with index='level',columns='status', values=sum of occurances with respect to the column and index

my code:

df_['temp']=df_['level'].astype(bool).astype(int)
df_.pivot(index='level',columns='status',values='temp')

but gives me, ValueError: Index contains duplicate entries, cannot reshape

My expected output is,

      open closed rejected
high  2    1      0
med   0    1      1
low   1    0      0

Please check and tell me if there is any other easy way.

Upvotes: 0

Views: 1406

Answers (1)

muzzyq
muzzyq

Reputation: 904

A simpler approach would be to count the occurrences of name:

df_.pivot_table(values='name',
                index='level',
                columns='status',
                aggfunc='count',
                fill_value=0)

Upvotes: 3

Related Questions