pivot_table() का उपयोग
pivot_table एक spreadsheet-style summary table बनाता है जिसमें एक variable नीचे और दूसरा ऊपर होता है।
In this page:
Syntax
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()
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. #}
आम गलतियां
- यह भूल जाना कि default aggfunc mean है
- pivot को duplicates पास करना
- खाली 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: