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.
The animation could not start (the drawing library did not load). The notes below contain the full graph as a table.
Every Pandas line in this table was run with pandas 2.3.3 on the small orders table used below.
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
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
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").
Colour dots show which Python basic each stop uses: data types operators functions class
None means "no value"?>, == and and?def function with a parameter and a return, and the same idea as a lambda?obj.method()?If you can tick all five, you are ready. Pandas is these same ideas applied to a whole table at once.
"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."