Skip to content
Advertisement

Transpose specific rows into columns in pandas

I have a dataset that has information like below:

Title                         Year                 Revenue (Mi)
---------------------------------------------------------------
Guardians of the Galaxy       2015                   313.13
Split                         2016                   138.0
Sing                          2016                   270.32
Moana                         2015                   248.02

I want to convert this piece of data to this form:

Title                        2015                      2016
------------------------------------------------------------
Guardians of the Galaxy     313.13
Split                                                 138.02
Sing                                                  270.32
Moana                       248.02

How can I convert my data frame to this form ?? I am not getting what should I do in this case.

Advertisement

Answer

Try with

out = df.pivot(index='Title', columns='Year', values='Revenue (Mi)').reset_index()

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement