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 transform my grouped DataFrame column type rows into multiple columns

I have the following DataFrame:

Id         Category        Amount
900000007  Schuur          8.0
900000009  Garage          24.0
           Dakkapel        5.0
900000015  Schuur          8.0
           Garage          20.0
           Dakkapel        5.0
900000019  Schuur          8.0
           Garage          18.0

So it’s a list of Id’s, a variable list of categories and an amount for each category that exists for that Id.

Now I want it to become 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

Id         Schuur       Garage    Dakkapel
900000007  8.0          NaN       Nan
900000009  Nan          24.0      5.0
900000015  8.0          20.0      5.0
900000019  8.0          18.0      Nan

So the categories become the name of the columns, with one row for each Id and the amount filled in for each category.
Please help me find the right function for this, thanks!

>Solution :

If first 2 columns are levels of MultiIndex use Series.unstack with select column Amount:

df1 = df['Amount'].unstack()
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