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

wide_to_long() का उपयोग

wide_to_long उन कई column groups को unpivot करता है जो एक जैसा naming pattern share करते हैं, जैसे score1 और score2।

In this page:

  1. wide_to_long()
Syntax
python
long_df = pd.wide_to_long(df, stubnames="stub", i="id_column", j="suffix_column")

wide_to_long()

pd.wide_to_long(df, stubnames, i, j) हर stub name से शुरू होने वाले और एक suffix पर खत्म होने वाले columns को rows में इकट्ठा करता है। यह year-by-year columns जैसे repeated measurements के लिए उपयोगी है। i row को पहचानता है और j extracted suffix को नाम देता है।

Note: अगर suffixes underscore से अलग किए गए हों तो sep="_" सेट करें।

उदाहरण: wide_to_long()

python
import pandas as pd

df = pd.DataFrame({"id": [1, 2], "score_2023": [10, 20], "score_2024": [15, 25]})
print(pd.wide_to_long(df, stubnames="score", i="id", j="year", sep="_"))

# Output:
#          score
# id year
# 1  2023     10
# 2  2023     20
# 1  2024     15
# 2  2024     25
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 column भूल जाना
  2. suffix regex के बिना suffixes का numeric न होना
  3. इसे melt के साथ confuse करना
चैप्टर सारांश
  • common stubs वाले column groups को handle करता है
  • i rows को पहचानता है
  • j suffix column को नाम देता है
  • sep separator सेट करता है
🔒

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.