1

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:

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!

2
  • 1
    What is print (df.index) ? Commented Oct 14, 2022 at 9:35
  • the index is 2000 x 1, so one column Commented Oct 14, 2022 at 9:39

1 Answer 1

1

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

df1 = df['Amount'].unstack()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.