← Back to Pandas Course | Chapter 7: Grouping & Aggregation | Lesson 7 of 7

Named Aggregation करना

Named aggregation आपको aggregate करते समय हर result column के लिए स्पष्ट नाम चुनने देता है।

In this page:

  1. Named aggregation
Syntax
python
df.groupby("column").agg(
    new_name=("value_column", "function_name"),
    other_name=("other_column", "function_name")
)

Named aggregation

agg को keyword arguments पास करें जहां हर value (column, function) की एक जोड़ी है, या pd.NamedAgg का उपयोग करें। keyword output column name बन जाता है। इससे flat, readable column names मिलते हैं और multi-level header की समस्या टल जाती है।

Note: Named aggregation के लिए pandas 0.25 या बाद का version चाहिए।

उदाहरण: Named aggregation

python
import pandas as pd

df = pd.DataFrame({"dept": ["A", "A", "B"], "salary": [100, 300, 200], "age": [30, 40, 50]})
out = df.groupby("dept").agg(
    avg_salary=("salary", "mean"),
    max_age=("age", "max"),
    headcount=("salary", "count"),
)
print(out)

# Output:
#       avg_salary  max_age  headcount
# dept
# A          200.0       40          2
# B          200.0       50          1
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. tuple की जगह plain string पास करना
  2. एक ही output name को दोहराना
  3. key को column के रूप में चाहिए तो index reset करना भूल जाना
चैप्टर सारांश
  • kwargs नए नाम को (column, func) से map करते हैं
  • Output columns flat होते हैं
  • NamedAgg स्पष्ट form है
  • pandas 0.25 या बाद का version चाहिए
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.