Home >Backend Development >Python Tutorial >How Can I Transform Columns into Rows Using Pandas melt()?

How Can I Transform Columns into Rows Using Pandas melt()?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-21 13:35:09428browse

How Can I Transform Columns into Rows Using Pandas melt()?

Convert Columns into Rows with Pandas

In data analysis, it can be useful to restructure datasets to improve readability and analysis. A common transformation involves converting columns into rows. This article addresses how to perform this operation using the Pandas library.

Consider a dataset with location and date-specific information stored in column headers, as shown below:

| location | name | Jan-2010 | Feb-2010 | March-2010 |
|---|---|---|---|---|
| A        | "test" | 12 | 20 | 30 |
| B        | "foo"  | 18 | 20 | 25 |

The goal is to reshape the data into a format where each date corresponds to a row, as follows:

| location | name | Date | Value |
|---|---|---|---|
| A        | "test" | Jan-2010 | 12 |
| A        | "test" | Feb-2010 | 20 |
| A        | "test" | March-2010 | 30 |
| B        | "foo"  | Jan-2010 | 18 |
| B        | "foo"  | Feb-2010 | 20 |
| B        | "foo"  | March-2010 | 25 |

To achieve this transformation, Pandas provides the melt function. Simply apply melt to the DataFrame, specifying the columns to keep as row identifiers (id_vars) and the column headers for the new columns (var_name and value_name).

import pandas as pd

df.melt(id_vars=["location", "name"],
        var_name="Date",
        value_name="Value")

For Pandas versions prior to 0.20, a slightly different approach is required, involving a combination of melt and sort:

df2 = pd.melt(df,
                  id_vars=["location", "name"], 
                  var_name="Date",
                  value_name="Value")

df2 = df2.sort(["location", "name"])

Remember to reset the index to ensure a clean output:

df2.reset_index(drop=True)

This technique offers a convenient way to reshape dataframes, facilitating analysis and presentation of tabular data.

The above is the detailed content of How Can I Transform Columns into Rows Using Pandas melt()?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn