# 11.3 pivot_table: long मधून wide summary

Source: https://ravindrabagale.com/mr/datascience/ch11-reshape-and-wide-long-data/11-3-pivot-table-long-to-wide-summary.html
Language: mr (Marathi with English technical terms)

Transactional data मधून city rows आणि month columns असलेली amount summary करू:

# From transactional long data
tx = pd.DataFrame({
    "city": ["Pune", "Pune", "Nashik", "Pune", "Nashik", "Nagpur"],
    "month": ["Sep", "Oct", "Oct", "Oct", "Sep", "Oct"],
    "amount": [320, 900, 450, 400, 200, 240],
})
pt = pd.pivot_table(
    tx, index="city", columns="month", values="amount",
    aggfunc="sum", fill_value=0,
)
pt

aggfunc="sum" समान city-month च्या values एकत्र करते. fill_value=0 रिकाम्या summary cells मध्ये 0 दाखवते. एकूण totals हवे असतील तर margins वापरा:

# Margins = totals
pd.pivot_table(tx, index="city", columns="month", values="amount",
               aggfunc="sum", margins=True, margins_name="Total")

रवींद्र बागले यांची tip

pivot ला index+column जोड्या unique लागतात; duplicate असतील तर error येतो. त्या values एकत्र करायच्या असतील तर pivot_table वापरा.
