📘 How does pandas shape tabular data?
A Series is a one-dimensional array of values paired with a labeled Index, while a DataFrame is a two-dimensional table whose columns are Series sharing a common row Index. Both structures carry their labels with them, so the Index is not m
What you’ll learn
- Series, DataFrame & the IndexExplain how Series, DataFrame, and the Index relate as labeled data structures.A Series is a one-dimensional labeled array and a DataFrame is a two-dimensional table whose columns are Series sharing a common row Index. Both row and column labels are stored in first-class Index objects along axis=0 and axis=1. The Index drives lookup and alignment and can hold integers, strings, datetimes, or be hierarchical. DataFrames are commonly built from dicts, lists of records, or arrays, and core attributes (shape, index, columns, dtypes) let you inspect structure before transforming it.
- Dtypes & VectorizationReason about column dtypes and use vectorized, label-aligned operations.Each Series has a single homogeneous dtype (int64, float64, bool, object, etc.); object often holds Python strings and is slow. Adding a NaN to an int64 column upcasts it to float64 unless a nullable dtype is used. Vectorized operations apply element-wise in compiled code and far outperform Python row loops. Arithmetic aligns operands on the Index, yielding NaN for non-overlapping labels. Use astype, to_numeric, and to_datetime to convert types deliberately.
- Selection: loc, iloc & Boolean MasksSelect and filter data correctly with loc, iloc, and boolean masks.loc selects by label with inclusive stop labels, while iloc selects by integer position with exclusive stop, matching Python conventions. Boolean masks come from comparisons and filter rows; multiple conditions combine with parenthesized bitwise & | ~, not the and/or keywords. loc can take a boolean row mask and a column list together for conditional selection and safe assignment. Selecting one column with single brackets returns a Series, while a list of names returns a DataFrame.
- Missing Data & Method ChainingDetect and handle missing data and write safe method chains.Missing numeric data is represented as NaN, detected with isna/notna because NaN is not equal to itself. dropna removes and fillna replaces missing values, each an intentional choice. Most aggregations skip NaN by default, affecting count and mean. Because most methods return new objects, transformations chain naturally into readable pipelines. Chained indexing assignment like df[mask]['col']=x risks the SettingWithCopy pitfall; assign through a single df.loc[mask,'col']=x instead.
- Groupby & Combining DataApply split-apply-combine and combine tables with merge, join, and concat.Groupby follows split-apply-combine: split by keys, apply a function per group, and combine results, with keys typically becoming the result Index. Grouped objects support aggregate (summary per group), transform (aligned to original rows), and filter (keep qualifying groups). merge joins on key columns with inner/left/right/outer semantics; join merges on the Index by default; concat stacks along an axis. Non-unique merge keys produce a cross product that can inflate row counts, so verify key uniqueness.
- Reshaping: Pivot & MeltReshape data between wide and long form using pivot, melt, and stack.Data is wide when each variable has its own column and long (tidy) when one column names the variable and another holds the value. melt unpivots wide to long by keeping id_vars and gathering the rest. pivot reshapes long to wide and requires unique index-column pairs. pivot_table aggregates duplicate cells (defaulting to mean), combining grouping with reshaping. stack and unstack move labels between the row Index and the columns and pair naturally with a MultiIndex.
- Guided Project: Sales Summary PipelineBuild and self-review a correct pandas pipeline that summarizes sales data.Load a tidy sales table, inspect shape/dtypes/head, and parse dates with to_datetime. Clean missing revenue with a justified dropna or fillna, making any conditional update through df.loc to avoid SettingWithCopy. Group by category and aggregate revenue, then reshape with pivot_table or unstack into a category-by-region cross-tab. Finish with a peer-style critique: confirm intentional loc/iloc use, no copy-warning, stable row counts after any merge, correct final shape, and reproducibility from a clean run.
Questions this course answers
What best describes the relationship between a DataFrame and a Series?
A DataFrame's columns are Series that all share the same row Index; a Series is one-dimensional with its own labeled Index.
Why is the Index considered a first-class axis rather than just row numbers?
The Index stores axis labels of any type and drives label-based lookup and automatic alignment, so it is data, not incidental row position.
When you build a DataFrame from a dict of Series with differing indexes, what happens?
Construction from a dict of Series aligns on the union of indexes, inserting NaN where a label is absent in some input.
An int64 column gains a single NaN. What is the default result?
Default NumPy integer arrays cannot hold NaN, so pandas upcasts the column to float64 (nullable Int64 is an opt-in alternative).
Why prefer vectorized Series operations over Python for-loops over rows?
Vectorized operations apply element-wise in optimized compiled code, avoiding the per-row overhead of a Python loop.
Adding two Series that share only some index labels produces what for non-overlapping labels?
Arithmetic aligns operands on their labels; labels present in only one operand yield NaN in the result.
Grounded in trusted sources
- pandas documentation, 'Intro to data structures' (official pandas user guide)
- Wes McKinney, 'Python for Data Analysis', 3rd ed., 2022, ch. 5
- Jake VanderPlas, 'Python Data Science Handbook', 2nd ed., 2023, ch. 3
- pandas documentation, 'Essential basic functionality' and 'Nullable integer data type' (official user guide)
- pandas documentation, 'Indexing and selecting data' (official user guide)
- pandas documentation, 'Working with missing data' and 'Returning a view versus a copy' (official user guide)
Every Wunder lesson is built from real, reputable sources — never invented.
Related courses
Wunder is a personalized learn-anything platform — tell it any topic and it builds a beautiful, fact-checked course in minutes, with narration, a knowledge check, and a college-style University track.
© 2026 Wunder Learning LLC · Terms & Privacy