Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती

11.3 pivot_table: long मधून wide summary

रवींद्र बागले यांच्या course वर आधारित · सहज मराठीत explanation

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")