From Python basics to Pandas: the bridge graph

You already know data types, operators, functions and classes. This graph shows what each one turns into in Pandas, so you start Pandas from what you know. Press 🔊 Voice to hear every step explained.

1

The full bridge: Python basic → Pandas

Every Pandas line in this table was run with pandas 2.3.3 on the small orders table used below.

All four basics in one small program

This one program uses a dict and lists (data types), *, >=, != and & (operators), def and apply (functions), and the DataFrame class with its methods and attributes.

import pandas as pd                        # module: bring in the pandas toolbox

orders = {                                 # dict of lists (data types)
    "city":  ["Hyderabad", "Pune", "Vijayawada", "Pune"],
    "qty":   [2, 1, 3, 2],
    "price": [250.0, 180.0, None, 320.0],
}
df = pd.DataFrame(orders)                  # class -> object

df["price"] = df["price"].fillna(200.0)    # None becomes NaN, then we fill it
df["total"] = df["qty"] * df["price"]      # arithmetic on a whole column

def add_gst(amount):                       # your own function
    return round(amount * 1.18, 2)

df["with_gst"] = df["total"].apply(add_gst)

big = df[(df["qty"] >= 2) & (df["city"] != "Pune")]   # comparison + logical
print(big)
print(df.groupby("city")["with_gst"].sum())            # methods, one after another
print(df.shape)                                         # attribute (a tuple)

Real output

city qty price total with_gst 0 Hyderabad 2 250.0 500.0 590.0 2 Vijayawada 3 200.0 600.0 708.0 city Hyderabad 590.0 Pune 967.6 Vijayawada 708.0 Name: with_gst, dtype: float64 (4, 5)

Watch out: two places where Python habits break

1. and / or do not work on whole columns. Use & and |, and put each condition in brackets.

df[(df["qty"] > 1) and (df["price"] < 300)]   # wrong
df[(df["qty"] > 1) & (df["price"] < 300)]     # right

Real error from the wrong line

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

2. Most methods return a new table. Just like a function's return, df.sort_values("price") gives back a new DataFrame and leaves df unchanged. Save it: df = df.sort_values("price").

Your Pandas learning order

Colour dots show which Python basic each stop uses: data types operators functions class

Self check: are you ready for Pandas?

If you can tick all five, you are ready. Pandas is these same ideas applied to a whole table at once.

Say it in an interview

"Pandas builds on core Python. A DataFrame is a class, and a df is an object with attributes like shape and methods like groupby. I create it from a dict of lists. Operators work on whole columns, so comparisons become filters. And my own functions run on every row with apply."