Skip to content

Empirical Applications

The first step in many data-analysis projects is to organise information into a table. In pandas, this table is called a DataFrame. A DataFrame is similar to a spreadsheet, since it's arranged into rows and columns, but it's considerably more powerful, because every operation performed on it is written as code, which means it can be repeated exactly, checked for errors, and modified at any time without starting from scratch.

In business and management contexts, DataFrames might contain stock prices, accounting variables, company identifiers, student records, customer transactions, or economic indicators. As a general rule of good design, each column should represent a meaningful variable, and each row should represent a single observation. Getting this structure right early on makes every later step of analysis easier, since the shape and meaning of the data stays clear and predictable.

A.1 From Dictionaries to DataFrames

A common and convenient way to create a DataFrame from scratch is to begin with a Python dictionary. The dictionary's keys become the column names, and the corresponding values become the data stored in each column. This connects directly back to the dictionary concepts introduced in Session 2, only now applied to genuinely tabular data.

The example below uses student GPA data to keep things concrete and easy to follow, but exactly the same structure applies to business and management datasets more generally. For instance, students could just as easily be replaced by firms, subjects by product categories, and GPA values by performance measures recorded across two reporting periods.

Python Code Examples: Creating and Inspecting a DataFrame

import pandas as pd

# A small management-style dataset:
# term 1 and term 2 GPAs for students across subjects
data = {
    "Student": [1, 1, 1, 2, 2, 2, 3, 3, 3],
    "Subject": [1, 2, 3, 1, 2, 3, 1, 2, 3],
    "GPA_1":   [2.8, 2.9, 2.2, 2.0, 1.8, 1.9, 2.2, 2.3, 2.1],
    "GPA_2":   [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
}

df = pd.DataFrame(data)

print(df.head())
print(df.shape)
print(df.columns)
print(df.dtypes)

# Add a new variable that measures improvement between terms
df["GPA_Change"] = df["GPA_2"] - df["GPA_1"]

# Sort students by improvement, largest gains first
df_sorted = df.sort_values("GPA_Change", ascending=False)

print(df_sorted.head())

Expected output / what to inspect

   Student  Subject  GPA_1  GPA_2
0        1        1    2.8    3.4
1        1        2    2.9    3.8
2        1        3    2.2    2.9
...

When you run this yourself, take a moment to look closely at:

  • The rows and columns — does the shape (df.shape) match what you'd expect from nine students-subject combinations and four original variables?
  • The column names (df.columns) — are they exactly what you intended, with no typos or unexpected whitespace?
  • The data types (df.dtypes) — are the GPA columns being read as decimal numbers (float64), and the Student/Subject columns as whole numbers (int64)? A column silently loaded as text (object) instead of a number is one of the most common early data-preparation surprises.
  • The newly created GPA_Change column — does subtracting GPA_1 from GPA_2 produce the improvement figures you'd expect by eye?

Why start from a dictionary? Building a small DataFrame by hand like this, rather than immediately loading a large external file, is a genuinely useful skill in its own right. It's the fastest way to construct a toy dataset for testing an idea, prototyping a calculation, or creating a minimal, reproducible example when asking a colleague (or a course instructor) for help with a bug.

Connecting back to Session 2: notice that data here is just an ordinary Python dictionary, exactly the structure introduced in Session 2's section on dictionaries, where each key mapped to a single value. The only difference here is that each value is now a list of several entries rather than a single number, which is precisely what allows pd.DataFrame() to interpret each key as a full column, rather than a single cell.