0Pricing
Pandas & NumPy Academy · Ders

melt: Geniş Biçimden Uzun Biçime

id_vars ve value_vars belirterek geniş bir DataFrame'i pd.melt ile uzun biçime dönüştürün.

melt: Geniş Biçimden Uzun Biçime, CoddyKit'te ücretsiz bir Pandas & NumPy Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Pandas & NumPy Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Pandas & NumPy Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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  160

Basic 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    160

Selecting 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    160

melt() 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.

Sıkça Sorulan Sorular

“melt: Geniş Biçimden Uzun Biçime” dersi ücretsiz mi?

Evet — “melt: Geniş Biçimden Uzun Biçime” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Pandas & NumPy Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Pandas & NumPy Academy kursu toplamda 4 dersten oluşur.

“melt: Geniş Biçimden Uzun Biçime” dersinde ne öğreneceğim?

id_vars ve value_vars belirterek geniş bir DataFrame'i pd.melt ile uzun biçime dönüştürün. Pandas & NumPy Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Pandas & NumPy Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Pandas & NumPy Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.

“melt: Geniş Biçimden Uzun Biçime” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Pandas & NumPy Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Pandas & NumPy Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. pivot_table: Çapraz Tablo
  2. melt: Geniş Biçimden Uzun Biçime
  3. MultiIndex ile stack ve unstack
  4. Frekans Tabloları için crosstab
← Pandas & NumPy Academy Sayfasına Dön