← Back to Pandas Course | Chapter 9: Reshaping Data | Lesson 1 of 6

melt() का उपयोग

melt wide data (कई columns) को long data (हर measurement के लिए एक row) में बदल देता है।

In this page:

  1. melt()
Syntax
python
long_df = pd.melt(df, id_vars=["id_column"], value_vars=["column1", "column2"],
                  var_name="variable", value_name="value")

melt()

melt(id_vars, value_vars, var_name, value_name) columns को rows में unpivot करता है। id_vars identifiers के रूप में रहते हैं, जबकि बाकी एक variable column और एक value column बन जाते हैं। Long format वह है जिसे कई plotting और grouping tools पसंद करते हैं।

Note: अगर आप value_vars छोड़ दें, तो हर non-id column melt हो जाता है।

उदाहरण: melt()

python
import pandas as pd

wide = pd.DataFrame({"name": ["Ann", "Bob"], "math": [90, 80], "art": [70, 85]})
print(wide.melt(id_vars="name", var_name="subject", value_name="score"))

# Output:
#   name subject  score
# 0  Ann    math     90
# 1  Bob    math     80
# 2  Ann     art     70
# 3  Bob     art     85
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. id_vars भूल जाना जिससे identifiers भी melt हो जाएं
  2. नए columns को नाम न देना
  3. melt को pivot के साथ confuse करना
चैप्टर सारांश
  • melt wide से long में जाता है
  • id_vars identifiers के रूप में रहते हैं
  • var_name और value_name नए columns को label करते हैं
  • pivot का विपरीत
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.