1

Let's say I have a DataFrame:

   ID  Y-M  Question Score
0   1  2020-1   A    0.1
1   1  2019-2   B    0.5
2   2  2019-5   A    0.7
3   2  2020-2   B    0.9

I would like to transform to the dataframe like so

             2019-2             2019-5            2020-1       2020-2
             A     B            A    B            A    B       A     B          
ID    
    1        NAN   0.5          NAN  NAN          0.1   NAN    NAN    NAN                                          
    2        NAN   NAN          0.7  NAN          NAN.  NAN    NAN    0.9 

Is it possible? If yes, how do I do that?

1 Answer 1

1

Use pivot_table as follows.

pd.pivot_table(df, values = 'Score', index = 'ID', columns = ['Y-M', 'Question'])

The result is

enter image description here

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.