Series & DataFrames

ECON 3209 · Week 4, Lecture 1 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. Create pandas Series and DataFrame objects
  2. Inspect rows, columns, and data types
  3. Use loc and iloc for labelled and positional indexing
  4. Add, modify, and summarise columns in a DataFrame
  5. Connect tabular data handling to agricultural and banking applications

Why pandas?

  • Real datasets usually have variable names and row labels
  • NumPy arrays are fast, but they do not carry rich labels by default
  • pandas adds labels, missing-data handling, and table operations
  • It is the standard Python library for applied data wrangling
  • Most econometrics workflows begin with a pandas DataFrame

Think of pandas as a programmable spreadsheet that is easier to audit, repeat, and scale.

Creating a Series

  • A Series is a one-dimensional labelled object
  • It contains values and an index
  • It is useful for a single variable such as yield, price, or loan volume
  • Methods like .mean() work directly on the data

Creating a DataFrame

  • A DataFrame is a two-dimensional table
  • Columns can represent different variables
  • Each row is one observation such as a district or branch
  • DataFrames are ideal for applied econometrics data preparation

Inspecting Structure

  • .head() shows the first rows
  • .info() reports column names, non-null counts, and data types
  • Always inspect a dataset before analysis
  • Early inspection prevents many downstream mistakes

Selecting Columns and Rows

  • A single column selection returns a Series
  • Double brackets return a smaller DataFrame
  • Clear column selection is the first step in focused analysis
  • Use explicit names instead of guessing positions whenever possible

loc for Label-Based Indexing

  • loc selects by row and column labels
  • It is useful when labels carry meaning, such as district names
  • loc supports lists of labels and slices
  • In applied work, label-based indexing is often the safest choice

iloc for Position-Based Indexing

  • iloc selects by integer position
  • It behaves more like NumPy slicing
  • This is useful when you want “first two columns” or “rows 2 to 4”
  • Be careful: positions can change if the DataFrame is rearranged

Creating New Columns

  • New variables are easy to create from existing columns
  • The calculation happens element-wise by row
  • This is how we build indicators and ratios for analysis
  • Derived columns should be clearly named and documented

Filtering Rows

  • The condition creates a boolean mask
  • The DataFrame keeps only rows where the mask is true
  • This is a standard way to subset data before charts or models
  • Filtering is more reproducible than manual spreadsheet deletion

Summary Statistics in pandas

  • .describe() quickly summarises numeric columns
  • It reports count, mean, standard deviation, and key percentiles
  • This is a useful first diagnostic for any new dataset
  • pandas integrates descriptive statistics with tabular structure

Exercise

Create a DataFrame with three districts and columns for deposit_crore and loan_crore.

  1. Add a new column called gap equal to deposits minus loans.
  2. Use loc to print the row for one district.
  3. Filter rows where gap is greater than 20.

Summary

  • ✅ pandas adds labels and table structure to numerical data
  • Series handle one variable; DataFrame handles many variables together
  • loc uses labels and iloc uses positions
  • ✅ New columns can be created with simple vectorised expressions
  • ✅ Filtering and summary statistics are built into the workflow
  • ✅ pandas is the main entry point for real econometrics datasets

Next Lecture

Merge, Reshape & Groupby

  • We will learn how to combine datasets and summarise groups
  • These skills are essential for turning raw data into analysis-ready tables
  • They also mirror common tasks in official statistics and banking data systems