如何在 Pandas 中將寬資料轉換為長格式?

Barbara Streisand
發布: 2024-11-14 21:36:02
原創
771 人瀏覽過

How to Transform Wide Data to Long Format in Pandas?

Reshaping Wide Data to Long Format in Pandas

In the realm of data manipulation, reshaping data from wide to long format often arises as a necessity. Consider the following pandas dataframe:

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

To transform this into the desired long format:

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

We employ the pandas.melt or pandas.DataFrame.melt function, which elegantly transforms wide data into long format.

import pandas as pd

df = pd.DataFrame({
    'date' : ['05/03', '06/03', '07/03', '08/03'],
    'AA' : [1, 4, 7, 5],
    'BB' : [2, 5, 8, 7],
    'CC' : [3, 6, 9, 1]
}).set_index('date')

df = df.reset_index()
pd.melt(df, id_vars='date', value_vars=['AA', 'BB', 'CC'])
登入後複製

Alternatively, one can omit the reset_index step by calling melt with ignore_index=False:

dfm = df.melt(ignore_index=False).reset_index()
登入後複製

Resulting in the desired long format:

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

This transformation enables efficient merging with other dataframes based on shared dates and column names.

以上是如何在 Pandas 中將寬資料轉換為長格式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板