wide_to_long() का उपयोग
wide_to_long उन कई column groups को unpivot करता है जो एक जैसा naming pattern share करते हैं, जैसे score1 और score2।
In this page:
Syntax
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()
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. #}
आम गलतियां
- id column भूल जाना
- suffix regex के बिना suffixes का numeric न होना
- इसे 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: