Home >Backend Development >Python Tutorial >How do you efficiently transform wide data to a long format in Pandas?

How do you efficiently transform wide data to a long format in Pandas?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-12 02:41:02353browse

How do you efficiently transform wide data to a long format in Pandas?

Reshaping Wide Data to Long in Pandas

Many data manipulation tasks require datasets to be in a specific format, often referred to as long or wide data. In pandas, reshaping from wide to long can be achieved through the pd.melt or DataFrame.melt functions.

Original Data:

Consider the following wide dataframe, where the dates are the index and the columns represent different variables:

         AA  BB  CC
date
05/03     1   2   3
06/03     4   5   6
07/03     7   8   9
08/03     5   7   1

Reshaping to Long:

To reshape this dataframe into a long format, where each row represents a single date and variable combination, we can use:

df = df.reset_index().melt(id_vars='date')

This transforms the dataframe into:

     date variable  value
0   05/03       AA      1
1   06/03       AA      4
2   07/03       AA      7
3   08/03       AA      5
4   05/03       BB      2
5   06/03       BB      5
6   07/03       BB      8
7   08/03       BB      7
8   05/03       CC      3
9   06/03       CC      6
10  07/03       CC      9
11  08/03       CC      1

Alternatively, the reset_index step can be omitted by specifying ignore_index=False in the melt function:

dfm = df.melt(ignore_index=False)

This ensures that the row index is preserved in the transformed dataframe.

The above is the detailed content of How do you efficiently transform wide data to a long format in Pandas?. 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