0

If I have a pandas Dataframe like such

enter image description here

and I want to transform it in a way that it results in

enter image description here

Is there a way to achieve this on the most correct way? a good pattern

1 Answer 1

1

Use a pivot table:

pd.pivot_table(df,index='name',columns=['property'],aggfunc=sum).fillna(0)

Output:

         price          
Property  boat dog house
name                    
Bob          0   5     4
Josh         0   2     0
Sam          3   0     0

Sidenote: Pasting in your df's helps so people can use pd.read_clipboard instead of generating the df themselves.

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.