Pandas for Biologists

Most biological data is a table - samples, genes, measurements. Pandas is how you handle tables in Python without tears. Learn it, then use it to finish Track 1.

๐ŸŸขโ†’๐ŸŸก Beginner โฑ๏ธ ~1 hr ๐Ÿ Python ยท pandas ๐Ÿงฌ Real-data project

Before you start

You can fetch records from NCBI (lesson 4) and run Python. In Colab, run !pip install biopython pandas first.

Learning objectives

By the end of this lesson you will be able to: build and inspect a pandas DataFrame, select rows and columns and filter your data, read real tabular files into a DataFrame, and summarise a table to answer a question.

The DataFrame: a spreadsheet in Python

Pandas' core object is the DataFrame - a table with named columns and rows. If you've used Excel or the R lesson, this will feel familiar.

import pandas as pd

genes = pd.DataFrame({
    "gene":   ["TP53", "BRCA1", "EGFR", "MYC"],
    "length": [19149, 81189, 188307, 5318],
    "chrom":  ["17", "17", "7", "8"],
})
genes.head()        # preview the first rows
genes["length"]      # pull out one column

The four moves you'll use constantly

# 1. FILTER rows by a condition
big = genes[genes["length"] > 10000]

# 2. SORT
genes.sort_values("length", ascending=False)

# 3. a new COLUMN computed from others
genes["length_kb"] = genes["length"] / 1000

# 4. SUMMARIZE - overall and per group
print(genes["length"].mean())
genes.groupby("chrom")["length"].mean()   # average length per chromosome

Why pandas is worth learning once

Filter, sort, compute, group, summarize - these five verbs answer most questions you'll ever ask of a data table, whether it's 4 genes or 40,000. Learn them here and they pay off in every later track, including the RNA-seq results table.

Reading real files

Real data usually arrives as a CSV. One line loads it:

df = pd.read_csv("my_data.csv")
df.info()       # columns, types, and missing values at a glance
โ˜… Track 1 project - the payoff

Fetch & summarize real genes

Time to combine everything in Track 1: pull real gene sequences from NCBI, then use pandas to build a clean summary table comparing them. This is your portfolio-ready deliverable.

โ–ถ Build it in a notebook

Run !pip install biopython pandas first.

Open a blank Colab โ†’

1Fetch three real genes

from Bio import Entrez, SeqIO
import pandas as pd

Entrez.email = "your.email@example.com"

accessions = {
    "insulin":      "NM_000207",
    "beta-globin":  "NM_000518",
    "APP":          "NM_000484",
}

seqs = {}
for name, acc in accessions.items():
    h = Entrez.efetch(db="nucleotide", id=acc, rettype="fasta", retmode="text")
    seqs[name] = str(SeqIO.read(h, "fasta").seq)
    h.close()

2Summarize with pandas

rows = []
for name, seq in seqs.items():
    n = len(seq)
    gc = (seq.count("G") + seq.count("C")) / n * 100
    rows.append({
        "gene": name, "length": n, "GC_percent": round(gc, 1),
        "A": seq.count("A"), "C": seq.count("C"),
        "G": seq.count("G"), "T": seq.count("T"),
    })

df = pd.DataFrame(rows)
df

3Ask questions of your table

df.sort_values("GC_percent", ascending=False)        # most GC-rich first
print("Mean GC%:", round(df["GC_percent"].mean(), 1))
print("Longest gene:", df.loc[df["length"].idxmax(), "gene"])

# save your summary - your deliverable
df.to_csv("gene_summary.csv", index=False)

4Visualize it

import matplotlib.pyplot as plt
df.plot(x="gene", y="GC_percent", kind="bar", legend=False,
        color="#5b9cf8", title="GC content by gene")
plt.ylabel("GC %"); plt.tight_layout(); plt.show()
Network blocked? If NCBI is unreachable, paste any three sequences directly into the seqs dictionary instead of fetching - every step from #2 onward works identically.

๐ŸŽ‰ You've finished Track 1!

You can now read sequence files, recognize every major format, manipulate sequences with Biopython, pull real data from public databases, and summarize it with pandas - and you built a portfolio-ready gene summary to prove it.

Put it on your portfolio. Save the notebook and gene_summary.csv to a GitHub repo with a short README explaining what it does. That's a real, shareable piece of work - exactly what the reproducibility-and-career track will help you polish.

The translate or fetch warned about something - did it fail?

Warnings (like a sequence length not being a multiple of three) aren't errors - the code still produced your table. Read the message, but if you got a DataFrame, you succeeded.

You've completed the data-literacy track

Back to the roadmap - pick your next track โ†’