Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Pivot table in Pandas

I’m trying to write code for pivot table using pandas,

Here is my sample excel file.

Name    Owner   Role    Can Edit    Can Read    Can Delete  Can download
John    Julan   Role1   Yes               No          No          No
Ricard  Julan   Role2   No                Yes         No          No
Sam     Hannah  Role2   No                 No         Yes         No
Julia   Hannah  Role1   No                 No         No          Yes
Katie   Julan   Role2   No                 Yes        No          Yes

and output should be like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired output

Here’s my code

import pandas as pd
import numpy as np
df = pd.read_excel('pivot.xlsx')
table = pd.pivot_table(df, values=['Can Edit','Can Read','Can Delete','Can download'], index=['Owner'],columns=['Role'], aggfunc=np.sum)

But i don’t get desired results

>Solution :

You almost had it but you forgot "Name" in the index:

pd.pivot_table(df, index=['Name', 'Owner'], columns=['Role'],
               values=['Can Edit','Can Read','Can Delete','Can download'],
               aggfunc=np.sum)

Note that you do not need to specify the values if all the other columns are used:

pd.pivot_table(df, index=['Name', 'Owner'], columns=['Role'], aggfunc=np.sum)

output:

              Can Delete       Can Edit       Can Read       Can download      
Role               Role1 Role2    Role1 Role2    Role1 Role2        Role1 Role2
Name   Owner                                                                   
John   Julan          No   NaN      Yes   NaN       No   NaN           No   NaN
Julia  Hannah         No   NaN       No   NaN       No   NaN          Yes   NaN
Katie  Julan         NaN    No      NaN    No      NaN   Yes          NaN   Yes
Ricard Julan         NaN    No      NaN    No      NaN   Yes          NaN    No
Sam    Hannah        NaN   Yes      NaN    No      NaN    No          NaN    No
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading