Method Chaining करना
methods को एक के बाद एक chain करना किसी recipe की तरह पढ़ा जाता है और ढेर सारे temporary variables से बचाता है।
In this page:
Syntax
result = (df
.query("condition")
.assign(new_column=lambda d: expression)
.groupby("column")
.agg("function_name"))
method chaining
क्योंकि ज़्यादातर methods एक नया DataFrame return करते हैं, आप उन्हें chain कर सकते हैं: df.query(...).assign(...).sort_values(...)। chain को लाइनों में बांटने के लिए इसे parentheses में wrap करें। pipe() आपको अपने खुद के functions को chain में डालने देता है।
Note:
backslashes के बिना lines तोड़ने के लिए पूरी chain को parentheses में wrap करें।
उदाहरण: method chaining
import pandas as pd
df = pd.DataFrame({"name": ["Ann", "Bob", "Cy"], "price": [10, 25, 40], "qty": [3, 1, 2]})
result = (
df.assign(total=lambda d: d["price"] * d["qty"])
.query("total > 25")
.sort_values("total", ascending=False)
.reset_index(drop=True)
)
print(result)
# Output:
# name price qty total
# 0 Cy 40 2 80
# 1 Ann 10 3 30
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. #}
आम गलतियां
- chains में inplace=True मिलाना
- बहुत लंबी अपठनीय chains
- debugging के लिए ज़रूरी intermediate result खो देना
चैप्टर सारांश
- Methods नए DataFrames return करते हैं
- readability के लिए उन्हें chain करें
- Parentheses line breaks की अनुमति देते हैं
- pipe custom functions डालता है
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: