Analysing data with pandas
Now we use pandas to actually analyse, filtering, grouping and combining, the same operations you learned in SQL, now in Python. The ideas are identical; only the syntax differs, which is why learning SQL first makes pandas feel familiar.
Filtering rows
To keep only certain rows, you write a condition inside square brackets:
us_sales = df[df["region"] == "US"]Read it as: "from df, keep the rows where the region column equals US, and store the result in a new DataFrame called us_sales." The double equals means "is equal to" (a single equals would mean "assign"). This is pandas' version of SQL's WHERE.
Grouping and aggregating
To total by category, pandas has groupby, which works just like SQL's GROUP BY:
by_region = df.groupby("region")["amount"].sum()\nprint(by_region)Step by step: groupby("region") splits the data into groups by region; ["amount"] picks the amount column; .sum() totals it within each group. The result is the revenue per region, the exact same answer as the SQL query SELECT region, SUM(amount) FROM sales GROUP BY region. Same logic, different tool.
Combining tables and new columns
pandas merge is the equivalent of a SQL JOIN, combining two DataFrames on a shared key. And you can create a calculated column easily:
df["with_tax"] = df["amount"] * 1.2This makes a new column called with_tax equal to each amount times 1.2 (adding 20 percent), in one line across every row, no dragging formulas down.
Choosing the right tool
You now know three tools, and the skill is matching each to the task. Use Excel for small, quick, visual, one-off work. Use SQL to pull and aggregate data from a database. Use Python/pandas for automation, large data, and anything you will repeat. Real projects often use all three in a pipeline: SQL pulls the data, pandas cleans and analyses it, Excel or a dashboard presents it.
- df[df["col"] == value] filters rows (pandas' WHERE).
- groupby + sum totals by category (pandas' GROUP BY).
- merge combines DataFrames (pandas' JOIN); new columns are one line.
- Match the tool to the task; real pipelines use Excel, SQL and Python together.