explode() का उपयोग
explode किसी list-like cell के हर item को उसकी अपनी row में बदल देता है।
In this page:
Syntax
df = df.explode("list_column")
explode()
जब किसी cell में list हो, तो explode हर element के लिए एक row बनाता है और बाकी columns को दोहराता है। reset_index call न करने तक original index दोहराया जाता है। यह tags या multi-valued fields को normalize करने का standard तरीका है।
Note:
comma-separated text को expand करने के लिए explode को str.split के साथ जोड़ें।
उदाहरण: explode()
import pandas as pd
df = pd.DataFrame({"post": ["p1", "p2"], "tags": [["py", "data"], ["sql"]]})
print(df.explode("tags").reset_index(drop=True))
s = pd.Series(["a,b", "c"]).str.split(",")
print(s.explode())
# Output:
# post tags
# 0 p1 py
# 1 p1 data
# 2 p2 sql
# 0 a
# 0 b
# 1 c
# dtype: object
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. #}
आम गलतियां
- यह भूल जाना कि index दोहराया जाता है
- non-list values को explode करना
- खाली lists के NaN बनने की योजना न बनाना
चैप्टर सारांश
- explode हर list item के लिए एक row बनाता है
- बाकी columns दोहराए जाते हैं
- reset होने तक index दोहराया जाता है
- खाली lists NaN बन जाते हैं
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: