melt:横持ち形式から縦持ち形式へ
pd.meltで横持ち形式のDataFrameを縦持ち形式に変換し、id_varsとvalue_varsを指定します。
「melt:横持ち形式から縦持ち形式へ」はCoddyKit上の無料Pandas & NumPy Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPandas & NumPy Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Pandas & NumPy Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Wide vs Long Data Formats
Data can be structured in two fundamental shapes. Wide format has one row per subject and spreads measurements across multiple columns — for example, separate columns for January, February, and March sales. Long format has one row per observation, with a column for the variable name and another for the value. Most Pandas analysis functions, machine learning libraries, and visualisation tools prefer long format, making melt() an essential transformation tool.
The pd.melt() Function
pd.melt(df) (also available as df.melt()) converts a wide DataFrame to long format. The key parameters are id_vars (columns to keep as identifiers, unchanged), value_vars (columns to unpivot into rows), var_name (name for the new variable column), and value_name (name for the new value column).
import pandas as pd
# Wide format: separate columns per month
df_wide = pd.DataFrame({
'product': ['A', 'B'],
'Jan': [100, 150],
'Feb': [120, 130],
'Mar': [140, 160]
})
print(df_wide)
# product Jan Feb Mar
# 0 A 100 120 140
# 1 B 150 130 160Basic melt() Call
Call melt() with id_vars set to the columns you want to keep as identifiers. Every other column is unpivoted: its name becomes a value in the new variable column and its cell values move to the value column. The number of output rows equals the number of input rows times the number of value columns being melted.
df_long = df_wide.melt(
id_vars='product',
var_name='month',
value_name='sales'
)
print(df_long)
# product month sales
# 0 A Jan 100
# 1 B Jan 150
# 2 A Feb 120
# 3 B Feb 130
# 4 A Mar 140
# 5 B Mar 160Selecting Which Columns to Melt
By default, every column not listed in id_vars is melted. Use value_vars to melt only a subset of columns. Columns not in id_vars or value_vars are dropped from the result. This is useful when your wide DataFrame contains a mix of time-series columns and other attributes you do not want to unpivot.
df_wide2 = pd.DataFrame({
'product': ['A', 'B'],
'category': ['Electronics', 'Clothing'],
'Jan': [100, 150],
'Feb': [120, 130],
'Mar': [140, 160]
})
# Melt only Jan and Feb, keep product and category
df_long2 = df_wide2.melt(
id_vars=['product', 'category'],
value_vars=['Jan', 'Feb'],
var_name='month',
value_name='sales'
)
print(df_long2)Resetting the Index After melt()
melt() preserves the original row indices by repeating them for each variable. The result often has a non-sequential index like [0, 1, 0, 1, 0, 1]. It is good practice to call reset_index(drop=True) immediately after melt() to get a clean sequential index from 0 to n-1 in the output.
df_long = df_wide.melt(id_vars='product', var_name='month', value_name='sales')
print('Before reset:', df_long.index.tolist())
# [0, 1, 0, 1, 0, 1]
df_long = df_long.reset_index(drop=True)
print('After reset:', df_long.index.tolist())
# [0, 1, 2, 3, 4, 5]melt() Is the Inverse of pivot()
melt() is conceptually the reverse of pivot() / pivot_table(). You can go from long to wide with pivot_table() and from wide back to long with melt(). This round-trip is often used in pipelines: receive data in wide format, reshape to long for Seaborn plotting or ML features, then optionally pivot back for a report table.
# long -> wide
table = df_long.pivot_table(values='sales', index='product',
columns='month', aggfunc='sum')
print(table)
# product Feb Jan Mar
# A 120 100 140
# B 130 150 160
# wide -> long again
long_again = table.reset_index().melt(id_vars='product', var_name='month', value_name='sales')
print(long_again.head())Using melt() for Seaborn Plotting
Seaborn's categorical and relational plots work best with long-format data. A typical workflow: start with a wide DataFrame that has a column per group, melt to long format so that one column identifies the group and another holds the values, then pass both to Seaborn's hue and x/y parameters. This is one of the most common reasons to use melt().
import seaborn as sns
import matplotlib.pyplot as plt
# Long format is required for sns.lineplot with hue
df_long = df_wide.melt(id_vars='product', var_name='month', value_name='sales')
# sns.lineplot(data=df_long, x='month', y='sales', hue='product')
# plt.show()
print('Long data ready for Seaborn:')
print(df_long)Naming the Output Columns
By default, melt() names the new variable column 'variable' and the value column 'value'. Always override these with meaningful names using var_name and value_name. Descriptive column names make downstream code easier to read and prevent confusion when a DataFrame has dozens of columns.
# Default: generic column names 'variable' and 'value'
default = df_wide.melt(id_vars='product')
print(default.columns.tolist())
# ['product', 'variable', 'value']
# Better: descriptive names
good = df_wide.melt(id_vars='product', var_name='month', value_name='revenue_usd')
print(good.columns.tolist())
# ['product', 'month', 'revenue_usd']Melting Survey Data
A classic use case for melt() is survey responses where each column is a question and each row is a respondent. Melting converts this wide format into long format with columns for respondent ID, question name, and answer value. You can then easily compute the distribution of answers for each question using groupby().
survey = pd.DataFrame({
'respondent': [1, 2, 3],
'Q1_rating': [5, 3, 4],
'Q2_rating': [4, 5, 3],
'Q3_rating': [2, 4, 5]
})
long = survey.melt(id_vars='respondent', var_name='question', value_name='rating')
print(long)
print(long.groupby('question')['rating'].mean())Sorting the Melted Result
After melting, the rows are ordered by the original column order (Jan, Feb, Mar for each row). You often want to sort by the identifier column first, then by the variable column. Use sort_values() on the melted DataFrame to achieve the order that makes most sense for your analysis or downstream processing.
df_long = df_wide.melt(id_vars='product', var_name='month', value_name='sales')
# Sort by product, then month
df_sorted = df_long.sort_values(['product', 'month']).reset_index(drop=True)
print(df_sorted)
# product month sales
# 0 A Feb 120
# 1 A Jan 100
# 2 A Mar 140
# 3 B Feb 130
# 4 B Jan 150
# 5 B Mar 160melt() in a Data Pipeline
In a typical data pipeline, melt() appears right after loading or cleaning wide-format data, before any analysis or modelling step. Place it early in your pipeline to get to long format quickly, then all subsequent operations (groupby, seaborn, sklearn) work on the clean long-format data without special-casing.
def load_and_reshape(filepath):
raw = pd.read_csv(filepath)
# Melt all month columns into long format
month_cols = [c for c in raw.columns if c.startswith('month_')]
long = raw.melt(
id_vars=[c for c in raw.columns if c not in month_cols],
value_vars=month_cols,
var_name='month',
value_name='value'
)
return long.reset_index(drop=True)Quick Check
Test your understanding of pd.melt wide-to-long transformation from this lesson.
Lesson Recap
In this lesson you learned: melt() converts wide to long format by unpivoting columns into rows; id_vars specifies which columns stay fixed and value_vars selects which columns to melt; var_name and value_name give descriptive names to the new columns; and long format is preferred for Seaborn, groupby, and ML pipelines. Next up we explore stack and unstack for reshaping MultiIndex DataFrames.
よくある質問
「melt:横持ち形式から縦持ち形式へ」レッスンは無料ですか?
はい。「melt:横持ち形式から縦持ち形式へ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Pandas & NumPy Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Pandas & NumPy Academyコースには全4レッスンが含まれています。
「melt:横持ち形式から縦持ち形式へ」で何を学びますか?
pd.meltで横持ち形式のDataFrameを縦持ち形式に変換し、id_varsとvalue_varsを指定します。 ブラウザで直接実行するハンズオンコードでPandas & NumPy Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Pandas & NumPy Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのPandas & NumPy Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「melt:横持ち形式から縦持ち形式へ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このPandas & NumPy Academyレッスンでコードを書いて実行できますか?
はい。すべてのPandas & NumPy Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- pivot_table:クロス集計
- melt:横持ち形式から縦持ち形式へ
- MultiIndexでのstackとunstack
- 度数表に使うcrosstab