Module 05: Intro to Pandas

September 28, 2026

Intro to Scientific Python

Content covered:

  1. Keeping Your Branch Up to Date
    1. git pull upstream main on main
    2. git rebase on personal
  2. Objects and Classes
  3. Intro to Pandas
    1. DataFrames
    2. Loading Data
    3. Selecting Data
    4. Summary Statistics
Intro to Scientific Python

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

  • Update main with git pull upstream main and rebase your personal branch onto it with git rebase
  • Describe what an object/class is
  • Create and inspect a pandas DataFrame
  • Load external data into a DataFrame
  • Select columns and filter rows in a DataFrame
  • Compute summary statistics on a DataFrame
Intro to Scientific Python

1. Keeping Your Branch Up to Date

Intro to Scientific Python

Updating main

We're adding a small extra git lesson today: keeping your personal branch up to date with the course.

Recall from Module 4: main only holds what's in the course repo, and your own work goes on a branch.

First, bring today's module into main:

cd ~/workspace/gsk-intro-scientific-python-course
git switch main
git pull upstream main

main now has today's module, but your personal branch was made from an older main, so it doesn't have it yet.

Intro to Scientific Python

git rebase

To bring your personal branch up to date:

git switch personal
git rebase main

git rebase main takes the commits on your branch and replays them on top of the latest main. Your notes stay, and now sit on top of today's module.

Intro to Scientific Python

git rebase, cont.

before:  A---B---C  main
              \
               X---Y  personal

after:   A---B---C  main
                  \
                   X'---Y'  personal

X and Y are your commits. After the rebase, they have new commit hashes, X' and Y', because they now start from C.

Intro to Scientific Python

Rebase Conflicts

If you edited a file that also changed in the course repo, git rebase stops with a conflict. git status shows which files.

  • git rebase --abort: stop, and put your branch back the way it was
  • Or fix the file, then git add it and run git rebase --continue

Recall from Module 3: editing the course files can cause conflicts. If you get stuck, abort, then raise your hand and we will help resolve the issue.

Intro to Scientific Python

Practice (5 min)

Let's work on these questions:

  1. On main, run git pull upstream main, then git log to see today's commits.
  2. Switch to personal and run git rebase main.
  3. Run git log again. Where are your commits now, compared to today's commits?
Intro to Scientific Python

2. Objects and Classes

Intro to Scientific Python

Open Today's Notebook

We'll use this notebook, notebooks/module_05.ipynb, for the rest of class.

conda activate gsk
jupyter lab

In Jupyter Lab, open notebooks/module_05.ipynb. It has today's code and a cell for each practice question.

Intro to Scientific Python

Classes

Classes, also called Objects, are containers in Python that can store values, known as attributes; and functions, known as methods

Intro to Scientific Python

We've Already Been Using Objects

arr = np.arange(24)
arr.shape          # an attribute
arr.reshape(4, 6)  # a method

fig, ax = plt.subplots()
ax.set_title("A title")  # a method on an Axes object

Attributes are accessed with a . and no parentheses. Methods are called with (), just like functions.

Intro to Scientific Python

Anatomy of a Class

Classes are defined using class keyword:

class MyClass:
    def __init__(self, attribute1):
        self.attribute1 = attribute1

__init__ runs when you create a new object, and self refers to that object.

obj = MyClass(5)
obj.attribute1
Intro to Scientific Python

Classes, cont.

You can also include functions that will be stored in the object by default too:

class MyClass:
    def __init__(self, attribute1):
        self.attribute1 = attribute1

    def stringify(self, value):
        return str(value)
Intro to Scientific Python

Practice (5 min)

Let's work on these questions:

  1. Define a class Sample that stores a name and a NumPy array of values.
  2. Add a method summary() that returns the mean and standard deviation of values.
Intro to Scientific Python

3. Intro to Pandas

Intro to Scientific Python

Pandas

Pandas is a dataframe library used to load, manipulate, and analyze data.

You can think of it as a way to program a spreadsheet.

Pandas is already installed in our gsk conda environment.

Intro to Scientific Python

Pandas, cont.

By convention, we import pandas as pd.

import pandas as pd
Intro to Scientific Python

Creating our first DataFrame

import pandas as pd

my_zoo = {
    'animal': ['panda', 'panda', 'lion', 'tiger', 'lion'],
    'sex': ['f', 'm', 'f', 'f', 'm'],
    'age': [1, 5, 2.5, 3, 3]
}

df = pd.DataFrame(my_zoo)
Intro to Scientific Python

A DataFrame is an Object

pd.DataFrame is a class. Just like MyClass(5), calling pd.DataFrame(my_zoo) creates a new object.

type(df)

The attributes and methods we'll use today all come from this class.

Intro to Scientific Python

DataFrames and Series

DataFrames are a collection of Series. Series are a collection of values of all the same data type.

You can think of a Series conceptually as a generalized version of a numpy.array.

A Series can be a collection of strings, a collection of floats, a collection of DateTime, and a host of other data types.

Intro to Scientific Python

Selecting a Column

You can index the contents of a DataFrame in the same way we indexed values from an array.

Instead of using the index for selecting the data, we can use the column name to return the values.

This is similar to how a dictionary works. Recall from Module 1: we look up a dictionary's values by their key.

df['age']
Intro to Scientific Python

Series Methods

These Series have built-in methods to make sense of the data

df['age'].max()   # 5.0, the oldest animal

df['age'].mean()  # 2.9, the average age

Each of these returns a single value for the whole column.

Intro to Scientific Python

Practice (5 min)

Let's work on these questions:

  1. Create a DataFrame from a dictionary with three columns: sample, condition, and measurement.
  2. Select the measurement column and compute its .max() and .mean().
  3. Check the type() of your DataFrame and of the column you selected.
Intro to Scientific Python

Loading Data

You can also use Pandas to load data from files.

df = pd.read_csv('data/gapminder.tsv', sep='\t')

sep refers to the separator. For this file, the columns are delimited using tabs.

Intro to Scientific Python

Loading Data, cont.

Notice that you can import files using relative paths. data/gapminder.tsv means "the data folder next to this notebook, then gapminder.tsv inside it."

gsk-intro-scientific-python-course
└── notebooks
    ├── data
    │   └── gapminder.tsv
    └── module_05.ipynb
Intro to Scientific Python

Some Useful Methods

  • df.head(): returns a view of your data. By default, it'll show you the first five rows of your data.
  • df.tail(): similar to head but instead, it will return the last five rows of your data.

These both accept arguments. If you would like to return the first ten rows, you would write:

df.head(10)
Intro to Scientific Python

Some Useful Attributes

  • df.columns: returns a list of column names
  • df.shape: returns the number of rows and number of columns of your data.
  • df.dtypes: returns the data type of each column

Recall from Module 3: arrays have shape and dtype too.

Intro to Scientific Python

Practice (5 min)

Let's work on these questions:

  1. Load gapminder.tsv into a DataFrame called df.
  2. Show the first 10 rows and the last 3 rows.
  3. How many rows and columns does df have? What is the data type of each column?
Intro to Scientific Python

Selecting Data: Boolean Expressions

Recall from Module 3: rolls == 6 gave an array of True/False.

Pandas lets you do truth evaluation for a Series in your DataFrame by writing Boolean expressions.

df['continent'] == 'Africa'

This returns a Series that consists of True and False for the values in a given Series.

Intro to Scientific Python

Selecting Data: Boolean Expressions, cont.

We can use Boolean expressions to select a subset of data from our DataFrame

df[df['continent'] == 'Africa']

What this expression does is look at the column df['continent'] and see if the value in the column is equal Africa.

It only returns the rows where the Boolean expression is true.

Intro to Scientific Python

Selecting Data: Boolean Expressions, cont.

Pandas also has a convenient method for its DataFrames to write these queries.

df.query("continent == 'Africa'")

Notice that you need to provide a string to the query method. In this example, we are evaluating against a string therefore we need different string literals within our expression.

Intro to Scientific Python

Practice (5 min)

Let's work on these questions:

  1. Select all rows for a single country, e.g. 'Kenya'.
  2. Select all rows from 2007 and save them as df_2007. From df_2007, select the countries with a lifeExp above 75.
Intro to Scientific Python

Summary Statistics

  • .mean(): the average of the values
  • .median(): the middle value
  • .describe(): the count, mean, standard deviation, min, quartiles, and max of each numeric column
df['lifeExp'].mean()
df['lifeExp'].median()
df.describe()

Let's check out some of these summary statistics for our DataFrame.

Intro to Scientific Python

Practice (5 min)

Let's work on these questions:

  1. Compute the mean and median of lifeExp.
  2. Run df.describe(). Which columns show up, and which don't? Why?
  3. Using Boolean indexing, compare the mean lifeExp in 1952 and in 2007.
Intro to Scientific Python

Summary Statistics, cont.

Q: What do you notice about these values? Is there a way to understand the average life expectancy for a given country?

We'll answer this with group-apply-combine in Module 6.

Intro to Scientific Python

Save Your Work

Commit and push your notes from today on your personal branch:

git add notebooks/module_05.ipynb
git commit -m "Module 5 notes"
git push --force-with-lease origin personal

git rebase gave your commits new hashes, so a plain git push is rejected. --force-with-lease replaces the branch on your fork, but only if no one else has pushed to it since.

Intro to Scientific Python

That's it for today!

Intro to Scientific Python