Convert columns into rows with Pandas
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Handling and transforming data is a fundamental aspect of working with datasets in Python. Often, there may be a need to rearrange your dataset to better fit specific analytical tasks. One common transformation is converting columns into rows, also known as "unpivoting" or "melting" data. This article explores how to perform this operation using the Pandas library, a powerful data manipulation tool in Python.
Pandas Overview
Pandas is an open-source data analysis and manipulation library built on top of the Python programming language. It provides highly optimized performance with intuitive handling of large datasets. Pandas excels at two specific data structures:
- Series: A one-dimensional labeled array capable of holding any data type.
- DataFrame: A two-dimensional labeled data structure with columns of potentially different types, similar to a spreadsheet.
Melting Data in Pandas
The `melt()` function in Pandas is used to transform or reshape data. It primarily performs the conversion of columns into rows, making it very handy for tidying up datasets. Melting a dataset means that you convert a wide-format dataset to a long-format dataset.
Syntax
- frame: The DataFrame to melt.
- id_vars: Column(s) to use as identifier variables.
- value_vars: Column(s) to unpivot. If not specified, it uses all columns not in `id_vars`.
- var_name: Name to use for the 'variable' column. If None, uses `frame.columns.name` or ‘variable’.
- value_name: Name to use for the 'value' column.
- col_level: If columns are a MultiIndex, this level is melted.
- ignore_index: If True, original index is ignored. If False, the original index is retained.
0 2020 100 110 120 1 2021 150 160 170 2 2022 200 210 220
0 2020 January 100 1 2021 January 150 2 2022 January 200 3 2020 February 110 4 2021 February 160 5 2022 February 210 6 2020 March 120 7 2021 March 170 8 2022 March 220
- The `melt` function is highly versatile and can be customized to fit specific needs, such as handling multiple levels of index with `col_level`.
- The inverse operation of melting is pivoting, where rows are turned into columns using the `pivot` or `pivot_table` functions.

