Prepstellar

Data Analysis Fundamentals · Data Foundations and Loading

21 cards

Labeled Data Structures

Swipe, scroll or use ← →
  1. Choose the structure that matches the data

    Every analysis starts with a container decision, and getting it right early saves a great deal of repair work later. The question is not how many values do I have but how many labeled axes does this data need.

    A Series is a one-dimensional labeled array that can hold any data type. Its axis labels form the index, so a value remains associated with a label rather than only with a numeric position. A DataFrame is a two-dimensional labeled structure whose columns may have different data types. It has an index for row labels and columns for column labels.

    Series DataFrame
    Labeled axes One: the index Two: the index and columns
    Types One dtype for the whole object A dtype per column
    Natural fit A single measured variable A table of several variables
    1 / 21
  2. Choose the structure that matches the data

    Think of one weather station. Daily rainfall, one number per date, is a Series: each value stays tied to its date through the index. Add temperature, wind speed and a text quality flag for the same dates, and you now have four variables sharing one set of row labels — that is a DataFrame, and the text flag can sit next to the numeric columns because columns may carry different dtypes.

    The advantage of labels is that they survive operations. A value linked to the label 2026-03-11 keeps that link when the data is filtered, joined or reordered. A bare positional array offers no such guarantee, which is why the labeled structure is the starting point rather than an accessory.

    2 / 21
  3. Quick check

    A dataset holds one rainfall reading per date for a single station, and each reading must stay tied to its date. Which container fits, and why?

    1. AA DataFrame, because dated readings need two labeled axes

      A DataFrame carries row labels and column labels; one labeled variable does not need a second axis.

    2. BA Series, because it is one-dimensional and labels every value

      Right. A Series is a one-dimensional labeled array, so each reading stays attached to its date rather than to a slot.

    3. CA plain NumPy array, because dates can be read back from position

      A plain array has no index, so the dates would exist only as positions and any reordering could lose them.

    3 / 21

  4. Create a Series from a dictionary, an array, or a scalar

    A Series can be created from a dictionary, an array, or a scalar. The three inputs differ in one respect that matters more than any other: where the labels come from.

    Input Where the labels come from
    Array or list You supply an index, or pandas creates one for you
    Dictionary The dictionary keys become the labels
    Scalar You must supply an index; the value is spread across it

    When an array is paired with an explicit index, the index must have the same length; without one, pandas creates labels from zero through one less than the data length. So an array of five values with no index arrives labeled 0, 1, 2, 3, 4.

    4 / 21
  5. Create a Series from a dictionary, an array, or a scalar

    A scalar requires an index and is repeated to match that index. pd.Series(0.0, index=['a','b','c','d','e']) is therefore a compact way to create a five-value Series pre-filled with the same starting number — the index sets the length, and the single value is broadcast to fill it.

    A dictionary supplies labels from its keys, while an explicitly supplied index selects and orders matching dictionary values and introduces missing values for absent keys. That last behavior is the useful one: pass an index of the labels you want, and you get exactly those labels, in your order, with a missing value wherever the dictionary had nothing to offer. Nothing is invented and nothing extra sneaks in.

    5 / 21
  6. Quick check

    `pd.Series(0.0, index=['a','b','c','d','e'])` is evaluated. What comes back?

    1. AA Series of five values, the scalar repeated across every label

      Right. A scalar requires an index and is repeated to match it, so the five labels set the length of the result.

    2. BOne value at label 'a', with the remaining four labels dropped

      The supplied index determines the length, so every label receives the value rather than only the first.

    3. CFive separate DataFrame columns, one named after each label

      The constructor builds one Series; the labels become its index, not the columns of a table.

    6 / 21

  7. Create a DataFrame from labeled inputs

    A DataFrame accepts a wide range of inputs, which is convenient but makes it worth knowing what each one implies for the labels. Suitable inputs include dictionaries of one-dimensional arrays, lists, dictionaries or Series; a two-dimensional NumPy array; structured arrays; a Series; and another DataFrame.

    Two of those inputs cover most everyday work, and they behave differently:

    • In a dictionary of equal-length lists or arrays, dictionary keys become columns and pandas creates a range index unless explicit row labels are supplied.
    • In a dictionary of Series, pandas aligns the Series by label and uses the union of their indexes.

    A dictionary whose keys name variables is a direct way to construct DataFrame columns, and those columns may retain different dtypes.

    7 / 21
  8. Create a DataFrame from labeled inputs

    The difference is worth a moment. Lists carry no labels, so pandas can only stack them side by side and number the rows. Series do carry labels, so pandas honors them: rows are matched by label, and the result keeps every label seen in any of the inputs.

    Input to the dictionary Row labels of the result
    Equal-length lists or arrays A range index, unless you supply row labels
    Series with matching indexes That shared index
    Series with different indexes The union of their indexes

    So a dictionary holding one Series labeled a, b and another labeled b, c produces a table with rows a, b and c. No observed label is dropped, and a cell with no value on one side is simply marked missing.

    8 / 21
  9. Quick check

    Two Series go into a dictionary passed to `pd.DataFrame`. One is labeled `a,b`; the other is labeled `b,c`. Which row labels does the table get?

    1. AOnly `b`, since a row must hold a value in both columns

      Alignment does not restrict the result to shared labels; rows with one side absent are kept and marked missing.

    2. B`a` and `b` alone, because the first Series fixes the labels

      Later Series contribute their labels too, rather than being fitted into whatever the first input happened to have.

    3. C`a`, `b` and `c`, the union of the two indexes

      Right. A dictionary of Series is aligned by label, and the result uses the union of the indexes.

    9 / 21

  10. Keep your progress in the app

    That’s 3 of 8 quick checks. In the app they stay answered, and every lesson remembers where you left off.

  11. Make lengths and labels agree before you construct

    Constructors are strict about length for a good reason: a table whose columns disagree about how many rows exist is not a table. pd.Series([1, 3, 5]) produces a one-dimensional object with a default range index, and pd.DataFrame(array, index=rows, columns=cols) attaches explicit row and column labels to a two-dimensional array — but both only work when the pieces fit.

    When constructing from arrays or lists, all column arrays must have the same length, and any explicit index must match that length.

    Two habits prevent nearly every construction error:

    1. Reconcile the columns first, so they all report the same number of values.
    2. Build the index to that same length, then pass it.

    If two variable lists disagree — say four values against five — the fix belongs upstream in the data, not in the constructor. Neither padding the short list nor trimming the long one is something pandas will quietly do on your behalf, and neither is something you should do without deciding what the missing observation actually means.

    10 / 21
  12. Quick check

    You hold two named lists of measurements, one with four values and one with five. The table must keep all five observations and carry explicit row labels. What has to happen first?

    1. AMake both columns the same length, then supply five row labels

      Right. Column arrays must share one length, and an explicit index has to match that same length.

    2. BBuild the table now and rely on the shorter list being padded out

      A short column is not filled in for you; the mismatch is an error rather than something settled quietly.

    3. CGive four row labels and let the longer list be cut down to fit

      An index of the wrong length is no reason to throw away a real observation.

    11 / 21

  13. Expect alignment by label, not by position

    Operations between Series align values by index label. This is the behavior that most repays understanding, because it quietly does the right thing in cases where positional arithmetic would produce a plausible-looking wrong answer.

    An operation between differently indexed Series returns the union of their indexes, and a label missing from either operand produces a missing result at that label. This differs from treating the values as anonymous positions.

    12 / 21
  14. Expect alignment by label, not by position

    Take a series of readings labeled a, b, c and a second labeled b, c, d. Adding them gives four results:

    Label Left Right Result
    a present absent missing
    b present present the sum
    c present present the sum
    d absent present missing

    Two things happen here, and both are deliberate. Nothing observed is discarded: a and d stay in the result. And nothing is invented: where only one side has a number, the result says missing instead of guessing. Positional arithmetic would instead have paired a with b and b with c — three tidy numbers, all of them wrong.

    13 / 21
  15. Quick check

    Series `x` is labeled `a,b,c` and Series `y` is labeled `b,c,d`. What does `x + y` return?

    1. AThree results paired first with first, with the labels dropped

      Pairing by position ignores the labels, which is precisely the mistake alignment exists to prevent.

    2. BResults at `a`, `b`, `c` and `d`, missing where only one side has a value

      Right. The operation returns the union of the indexes, and a label absent from either side yields a missing result.

    3. CResults at `b` and `c` only, since both sides must supply a number

      Labels present on only one side are kept in the union rather than removed from the result.

    14 / 21

  16. Inspect the structure before analysis

    A short inspection before any real work tells you whether the object is what you think it is. Four attributes cover it, and each answers a different question.

    Attribute What it answers
    shape How large is each axis?
    index What are the Series labels or DataFrame row labels?
    columns What are the DataFrame column labels?
    dtypes What type is held in each DataFrame column?

    Use shape to inspect axis dimensions. Use index for Series labels and DataFrame row labels, and use columns for DataFrame column labels. Use dtypes to inspect the dtype of every DataFrame column; a Series has one dtype, since a Series has only one column of values to describe.

    Reading these four is also how you record the schema of a table before changing it — which matters most right before a conversion that will not preserve it.

    15 / 21
  17. Quick check

    You need the dimensions of a table, and then the type held in each of its columns. Which pair of attributes answers that?

    1. A`columns` for the dimensions, then `index` for the types

      `columns` holds column labels and `index` holds row labels; neither reports a size or a type.

    2. B`shape` for the dimensions, then `array` for the types

      `array` returns the ExtensionArray behind a single Series and does not report a type per column.

    3. C`shape` for the dimensions, then `dtypes` for the types

      Right. `shape` reports the axis dimensions, and `dtypes` reports the dtype of every column.

    16 / 21

  18. Ask for an underlying array only when you need one

    Sometimes a library outside pandas needs the raw values. There are two ways to hand them over, and they are not interchangeable.

    Use .array when the ExtensionArray backing a Series or Index is required. Use to_numpy() when a NumPy array is required.

    The second one has consequences. DataFrame.to_numpy() omits row and column labels and may coerce heterogeneous columns to a common NumPy dtype. That conversion may copy data, especially when the common dtype is object.

    17 / 21
  19. Ask for an underlying array only when you need one

    A NumPy array has a single dtype and no labels, so a mixed table cannot survive the trip intact. Text and numbers together must meet somewhere, and that common ground is usually object — which is also the case most likely to copy the data rather than share it.

    The order to work in follows from that:

    1. Inspect shape, index, columns and dtypes, so the schema is on record.
    2. Convert.
    3. Treat the resulting array as values only, and do not read its single dtype as evidence that the source columns already agreed.

    That last point catches people out. A uniform output dtype can be the result of coercion, not proof that coercion never happened. Inspect shape, labels, dtypes, and only then request an underlying array representation.

    18 / 21
  20. Quick check

    A table mixes text and numeric columns and carries meaningful labels, but an outside routine needs a plain NumPy array. What should you expect from `to_numpy()`?

    1. ARow and column labels travel with the array, and no data is ever copied

      Both labeled axes are omitted, and the conversion may copy data — most often when the shared dtype is `object`.

    2. BOnly the labels survive; the cell values stay behind in the table

      The values are exactly what the array carries; the labels are what does not come along.

    3. CLabels are dropped and mixed columns may be coerced to a single dtype

      Right. `to_numpy()` omits row and column labels and may coerce heterogeneous columns to a common NumPy dtype.

    19 / 21

  21. Key takeaways

    • Match the container to the axes: use a Series for one-dimensional labeled values and a DataFrame for two-dimensional labeled columns, where each column may hold a different dtype.
    • Know where the labels come from: an array takes an index you supply or a default range, a dictionary takes its keys, and a scalar requires an index and is repeated across it.
    • Match constructor inputs, explicit labels, and lengths before creating an object; a dictionary of equal-length lists gets a range index, while a dictionary of Series is aligned by label and keeps the union of the indexes.
    • Expect Series operations to align by labels rather than anonymous positions, returning the union and marking a label missing from either side.
    • Inspect shape, labels, dtypes, and only then request an underlying array representationto_numpy() drops the labels, may coerce mixed columns, and may copy.
    20 / 21
  22. Quick check

    Which statement matches how these structures actually behave?

    1. AA Series carries two labeled axes, and `to_numpy()` keeps the row labels

      A Series has one labeled axis, and the NumPy conversion omits both row and column labels.

    2. BA DataFrame allows a different dtype per column, and Series operations match by label

      Right. Per-column dtypes are the DataFrame's defining trait, and Series arithmetic aligns on index labels.

    3. CA dictionary of Series lines values up by position, and `shape` returns the column names

      A dictionary of Series is aligned by label, and `shape` reports axis dimensions rather than names.

    21 / 21

  23. 8 quick checks · then the test

    In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.

The whole course, on your phone

Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.