ECON 3209 · Week 4, Lecture 2 · Kerala Agricultural University
Autumn 2026
By the end of this lecture, you will be able to:
melt() and pivot_table()groupby()Merged table = observations matched on a common identifier such as (district, year)
on= tells pandas which key to matchhow="inner" keeps only matching rowsinner keeps matched rows onlyleft keeps all rows from the left tableright keeps all rows from the right tableouter keeps everything from both sidesmelt()melt() is the standard wide-to-long function in pandaspivot_table()pivot_table() often takes long data back to wide formatpivot_table() can aggregate themgroupby()groupby() splits data into groups.agg() lets us compute several summaries at onceA clean merge or groupby table is not only technical — it determines whether your final inference is trustworthy.
Create two small DataFrames:
bank_df with district and deposit_croreloan_df with district and loan_croreThen: 1. Merge them by district. 2. Create a gap column equal to deposits minus loans. 3. Use groupby() on a new region column to compute average gap.
melt() converts wide data to long formatpivot_table() helps reshape long data for reportsgroupby() is the core tool for grouped summariesECON 3209 — Kerala Agricultural University