1

I have a pandas dataframe that has a format like this

Date       Grade    Student
1/1/2015    0.15    Brian
1/1/2015    0.05    Steve
1/1/2015    0.02    Connor
1/2/2015    0.14    Brian
1/2/2015    0.05    Steve

.....

Is it possible to reformat the dataframe in such a way in that it now looks like this?

Date      Brian   Steve   Connor
1/1/2015   0.15    0.05    0.02
1/2/2015   0.14    0.05     -

I am quite familiar with Pandas and aggregating columns together but am unsure how to go about compressing them in this particular way, any help woiuld be great.

2 Answers 2

1

You can use pivot_table:

a = df.pivot_table(index='Date', columns='Student', values='Grade')

This returns:

Out[5]: 
Student   Brian  Connor  Steve
Date                          
1/1/2015   0.15    0.02   0.05
1/2/2015   0.14     NaN   0.05
Sign up to request clarification or add additional context in comments.

1 Comment

lol, I was just about to answer this myself as I had found a perfect example on my ext Google search. You are absolutely correct though. Many thanks.
1

In this simple case you can just use pivot.

>>> df.pivot('Date', 'Student', 'Grade')
Student   Brian  Connor  Steve
Date                          
1/1/2015   0.15    0.02   0.05
1/2/2015   0.14     NaN   0.05

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.