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

groupby() की बुनियादी बातें

groupby rows को किसी column के आधार पर groups में बांट देता है, ताकि आप हर group का अलग-अलग summary निकाल सकें।

In this page:

  1. groupby() basics
Syntax
python
df.groupby("column")
df.groupby("column")["value_column"].sum()

groupby() basics

df.groupby("col") एक lazy grouping object बनाता है। sum, mean या count जैसा aggregation apply करने पर हर group के लिए एक result compute होता है। यह split-apply-combine pattern को follow करता है। group keys result का index बन जाती हैं।

Note: group column को regular column के रूप में रखने के लिए as_index=False का उपयोग करें।

उदाहरण: groupby() basics

python
import pandas as pd

df = pd.DataFrame({"dept": ["A", "B", "A", "B"], "salary": [100, 200, 150, 250]})
print(df.groupby("dept")["salary"].sum())
print(df.groupby("dept", as_index=False)["salary"].mean())

# Output:
# dept
# A    250
# B    450
# Name: salary, dtype: int64
#   dept  salary
# 0    A   125.0
# 1    B   225.0
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. aggregation apply करना भूल जाना
  2. key को अनजाने में index में खो देना
  3. NaN वाले column से group करना, जो default रूप से drop हो जाता है
चैप्टर सारांश
  • groupby data को groups में बांटता है
  • Aggregations हर group पर चलते हैं
  • Keys index बन जाती हैं
  • as_index=False उन्हें column के रूप में रखता है
🔒

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.