Turning columns into rows
Unpivoting is a common operation done when reshaping or tidying your data. It helps turn and compact columns into rows. In other words, the unpivot operation converts your data from a wide format to a long format. This operation is also known as unpivot.
Both wide format and long format have their use cases. Some client tools prefer wide format while some prefer long format.
In this recipe, we’ll cover how to turn columns into rows using the unpivot operation.
How to do it...
Here’s how to turn columns into rows:
- Use the
.unpivot()
method to turn columns into rows:long_df = df.unpivot( Â Â Â Â index='academic_year', Â Â Â Â Â Â Â Â on=[ Â Â Â Â Â Â Â Â Â Â Â Â 'students', Â Â Â Â Â Â Â Â Â Â Â Â 'us_students', Â Â Â Â Â Â Â ...