← Back to Pandas Course | Chapter 13: Performance & Best Practices | Lesson 5 of 6

Method Chaining करना

methods को एक के बाद एक chain करना किसी recipe की तरह पढ़ा जाता है और ढेर सारे temporary variables से बचाता है।

In this page:

  1. method chaining
Syntax
python
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

python
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. #}
आम गलतियां
  1. chains में inplace=True मिलाना
  2. बहुत लंबी अपठनीय chains
  3. 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:

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.