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

pivot_table() का उपयोग

pivot_table एक spreadsheet-style summary table बनाता है जिसमें एक variable नीचे और दूसरा ऊपर होता है।

In this page:

  1. pivot_table()
Syntax
python
pd.pivot_table(df, values="value_column", index="row_column", columns="column_column", aggfunc="function_name")

pivot_table()

pivot_table(values, index, columns, aggfunc) index और columns से group करके values को aggregate करता है। default aggregation mean है। totals जोड़ने के लिए margins=True और NaN को replace करने के लिए fill_value का उपयोग करें।

Note: pivot_table duplicate entries को aggregate करके handle करता है, जबकि pivot error देता है।

उदाहरण: pivot_table()

python
import pandas as pd

df = pd.DataFrame({
    "region": ["N", "N", "S", "S", "S"],
    "product": ["a", "b", "a", "a", "b"],
    "sales": [10, 20, 30, 40, 50],
})
print(df.pivot_table(values="sales", index="region", columns="product", aggfunc="sum", fill_value=0, margins=True))

# Output:
# product   a   b  All
# region
# N        10  20   30
# S        70  50  120
# All      80  70  150
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. यह भूल जाना कि default aggfunc mean है
  2. pivot को duplicates पास करना
  3. खाली combinations को fill न करना
चैप्टर सारांश
  • pivot_table दो keys के बीच summarize करता है
  • Default aggfunc mean है
  • margins totals जोड़ता है
  • fill_value NaN को replace करता है
🔒

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.