Skip to content
intermediate Phase 19 · Python Data Science Basics

pandas Fundamentals (Data Science)

Work with DataFrames, Series, and perform data manipulation with pandas.

1h 30m
0 problems
Topic Progress 0%

Series, DataFrame, and Inspection

Series, DataFrame, and Inspection

A Series is a labeled 1-D array; a DataFrame is a dict of aligned Series sharing an index:

import pandas as pd

s = pd.Series([10, 20], index=["a", "b"], dtype="int64")
df = pd.DataFrame({"name": ["ann", "bo"], "score": [91, 84]})
df = pd.read_csv("scores.csv", usecols=["name", "score"])   # dtype inferred per column

df.head(); df.info(); df.describe()      # dtypes, nulls, five-number summary
df["score"]            # Series — always prefer bracket access
df.shape, df.columns, df.index

Column selection returns views of labels, not positions: df.loc["row1", "score"] is label-based, df.iloc[0, 1] is positional. Index alignment (a labeled Series keyed by id) is what makes vectorized joins between Series possible without manual matching.

Filter, Group, and Join

Filter, Group, and Join

top = df[df["score"] > 85]
tagged = df.assign(passed=df["score"] >= 60)

summary = (df.groupby("class")              # split-apply-combine
             .agg(mean_score=("score", "mean"),
                  n=("score", "size"))
             .sort_values("mean_score", ascending=False))

merged = df.merge(roster, on="student_id", how="left")   # SQL-style join
df.query("score > 85 and class == 'A'")    # readable filter syntax

groupby follows three phases — split by key, apply an aggregation/transform/filter, combine results. how selects the join: inner, left, outer, .join aligns on index. Combine with sort_values, drop_duplicates, and value_counts() for the tabulations interviewers ask you to produce live.

Missing Data and Mutation Pitfalls

Missing Data and Mutation Pitfalls

Missing values arrive as NaN (float columns) or pd.NA:

df.isna().sum()
df.dropna(subset=["score"])
df["score"] = df["score"].fillna(df["score"].median())

Chained assignment — df[df.a > 0]["b"] = 1 — may silently do nothing (SettingWithCopyWarning) because the filter produced a copy. Always assign through loc: df.loc[df["a"] > 0, "b"] = 1. Recent pandas (3.x) makes copy-on-write the default, so chained writes stop propagating instead of warning.

Index alignment: adding two Series pairs values by label — mismatches yield NaN. Reset with reset_index(drop=True) when labels are meaningless positions.

Interview angle: describe split-apply-combine, contrast merge (key join) with groupby (aggregate), and explain how copy-on-write removes an entire class of surprise mutations.

Quiz

1. What does df.loc[df.score > 85, "score"] = 0 do?

Question 1 options

2. Why might s1 + s2 produce NaNs even with no missing data?

Question 2 options

3. What are the three stages of groupby?

Question 3 options

Flashcards

Question

loc vs iloc

Answer

loc = label-based (masks, names); iloc = integer position. Mixed use causes off-by-one errors.

Question

split-apply-combine

Answer

groupby splits rows by key, applies agg/transform, combines into a new frame.

Question

Chained assignment

Answer

df[mask]["col"] = v writes to a copy — use df.loc[mask, "col"] = v instead.

Question

merge how=

Answer

inner/left/right/outer select which keys survive; how="left" keeps the left frame’s rows.

Revision Notes

Key Takeaways

  • 1. Series/DataFrame operations align on labels — that is both the power and the NaN trap
  • 2. Write with .loc, aggregate with groupby.agg, combine with merge
  • 3. Inspect with head/info/describe before transforming; control dtypes at read time

Interview Tips

  • • Derive split-apply-combine concretely: compute class averages in three chained calls
  • • Explain copy-on-write and why the old SettingWithCopyWarning existed

Cheat Sheet

df.loc[mask, col]=v; groupby().agg(named); merge(how="left"); fillna/dropna; Series ops align on index; avoid chained assignment.

Continue Learning