Prepstellar

Data Analysis Fundamentals · Getting started

20 cards

Core Pandas Vocabulary

Swipe, scroll or use ← →
  1. Series: one labeled dimension

    Everything in pandas is described with a handful of words, and using them precisely saves hours later. Start with the simpler of the two objects.

    A Series is a one-dimensional labeled array that can hold values of any data type: whole numbers, decimals, text, dates, or Python objects. A Series has one axis, and its axis labels are collectively called the Index. It also has one dtype, which describes the kind of values it stores.

    Index value
    Mon 21.4
    Tue 22.0
    Wed 19.8

    One axis, one set of labels, one kind of value. That is the whole definition.

    1 / 20
  2. Series: one labeled dimension

    The single dtype is worth pausing on, because it is the main difference between a Series and a table. A Series of temperatures is a Series of decimal numbers; a Series of city names is a Series of text. You cannot give one of its entries a type of its own.

    Term What it names
    Series the one-dimensional labeled container itself
    Index the labels of its single axis
    dtype the one kind of value it holds
    scalar a single value inside it, such as 21.4

    The Index is not a store of values, and the values do not carry their own labels around independently. The Series is what binds the two.

    2 / 20
  3. Quick check

    Which statement describes a Series?

    1. AIt has two axes and stores a different kind of value on each of those two axes

      Two axes make a DataFrame; a Series has one, and its values share a single type.

    2. BIt has one axis whose labels are the column names taken from a wider table

      The labels of a Series axis are its Index; column names belong to the second axis of a DataFrame.

    3. CIt has one axis, its labels are the Index, and it holds one `dtype`

      Right. One labeled axis called the Index, and one `dtype` for the values it stores.

    3 / 20

  4. DataFrame: two labeled axes

    Most analysis needs several variables together, which is what the second object is for. A DataFrame is a two-dimensional labeled data structure whose columns can have different data types. Think of it as a table, a spreadsheet, a SQL result, or a dictionary of Series.

    Its first semantic axis is the Index, which labels rows. Its second is columns, which contains the column labels. Those two axes give every cell an address made of labels rather than of numbers.

    4 / 20
  5. DataFrame: two labeled axes

    That address is the point. A DataFrame separates the position of a cell from the row and column labels that identify it, so a value is "the total of order 1002" and not "the value in the second row, fourth position".

    customer ordered_on total
    1001 Ana Ruiz 2024-03-01 249.50
    1002 Beca Lim 2024-03-02 89.00

    Here the Index holds the order IDs 1001 and 1002, and columns holds customer, ordered_on, and total. Each column keeps its own type — text, date, decimal — which a single-typed grid could not do.

    5 / 20
  6. Quick check

    Which description of a DataFrame is accurate?

    1. AA table whose rows are labeled by the Index and whose columns may hold different types

      Right. The Index labels the rows, `columns` labels the columns, and column types may differ.

    2. BA grid with one type for all of its cells and no labels on either of its two axes

      A DataFrame labels both axes, and its columns are allowed to hold different data types.

    3. CA one-dimensional sequence whose labels serve as its column names

      That is a Series: one dimension, and its labels are an Index rather than column names.

    6 / 20

  7. Rows, columns, and the labels on them

    Two more words carry analytical meaning, not just structural meaning. A row represents one observation across the DataFrame's columns. A column represents one variable across the rows and can be selected as a Series.

    In the orders table, the row labeled 1001 is one order seen from every angle; the column total is one variable seen across every order. Reading a table in those terms tells you immediately whether it is shaped for the question you are asking.

    7 / 20
  8. Rows, columns, and the labels on them

    The row Index and the column labels form the coordinate system used to identify data, and labels do not have to be numbers. Order IDs, customer codes, dates, and country names all make good labels.

    A default RangeIndex is created when a Series is built from values without an explicit index. Pass five values and no labels, and pandas supplies the labels 0 through 4 — one less than the length — so that the object always has an Index to align on.

    How the object is built Resulting row labels
    Values plus an explicit list of labels the labels you passed
    Values alone a RangeIndex, 0 to length minus one

    That default is a convenience, not a signal that labels are unnecessary. Replace it as soon as the data has a meaningful key.

    8 / 20
  9. Quick check

    You build a Series from a plain list of five values and pass no index. What labels does it get?

    1. AColumn labels copied straight from the five values that were in the list

      Values and labels are different things; the values are not reused as labels.

    2. BA RangeIndex running from 0 through 4

      Right. Without an explicit index, pandas creates a default RangeIndex from zero to one less than the length.

    3. CNone at all, because passing a list switches the Index off

      Every Series has an Index; when you do not supply one, pandas creates it for you.

    9 / 20

  10. Keep your progress in the app

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

  11. Structural checks before any analysis

    Before calculating anything, confirm that the object really has the dimensions, labels, and types you expected. Four attributes answer that, and each answers a different question.

    Attribute Question it answers
    shape how many rows and how many columns
    index what the row labels are
    columns what the column labels are
    dtypes what type each DataFrame column holds
    dtype what single type a Series holds

    Use shape to read the number of rows and columns. Use index to inspect row labels and columns to inspect column labels. Use dtypes to inspect the type of each DataFrame column; use dtype for the single type of a Series.

    10 / 20
  12. Structural checks before any analysis

    The singular and the plural are not interchangeable, and mixing them up is the most common slip in this vocabulary.

    Name Object What comes back
    dtype a Series one type, because a Series holds one kind of value
    dtypes a DataFrame one entry per column, because columns may differ

    These structural checks come before analysis because they reveal whether the object has the expected dimensions, labels, and types. A calculation on a column that silently arrived as text will not fail loudly; it will produce something that looks like a number and is not.

    11 / 20
  13. Quick check

    A table has customer IDs as row labels and four named variables, each of a different type. You must check both axis lengths and the type of every variable. Which pair of checks does that?

    1. A`dtype` for the axis lengths, and `index` for the variable types

      `dtype` reports a single type rather than any dimension, and `index` returns row labels rather than types.

    2. B`shape` for the axis lengths, and `dtypes` for the variable types

      Right. `shape` reports the two axis lengths, and `dtypes` reports the type of each named column.

    3. C`columns` for the row count, and `shape` for the type of each variable

      `columns` returns the column labels, and `shape` reports lengths rather than types.

    12 / 20

  14. Containers, not displays

    A useful mental model is that a DataFrame contains Series, and a Series contains scalar values. The objects are labeled containers, not merely displays of arrays: nesting runs from two dimensions, to one, to a single value.

    Level Object Holds
    2 DataFrame Series, one per column
    1 Series scalar values, one per label
    0 scalar a single value such as 89.00

    The model never runs the other way. A Series does not contain DataFrames, and an Index does not contain either of them.

    13 / 20
  15. Containers, not displays

    The container model has an immediate practical consequence. Selecting one DataFrame column therefore yields a Series while preserving its Index.

    Take total out of the orders table and you get a Series of decimals labeled 1001 and 1002 — the same row labels the table used. Nothing has to be re-attached afterwards, and the extracted variable can still be aligned against anything else that uses those order IDs.

    Selection Result Labels kept
    One column of a DataFrame a Series the DataFrame's row Index
    Several columns a narrower DataFrame rows and the chosen columns
    14 / 20
  16. Quick check

    You need one named variable across many labeled observations, and you must keep those row labels when you pull it out of a wider table. What should the selection give you?

    1. AA Series holding just that variable, still labeled by the wider table's row Index

      Right. Selecting one column narrows the table to one variable, and the Series keeps the DataFrame's row Index.

    2. BA DataFrame that carries every one of the original variables along with it

      Keeping every variable does not narrow the table to the one you need.

    3. CA scalar whose single label is meant to stand for all the observations

      A scalar is a single value and cannot represent many labeled observations.

    15 / 20

  17. Handing the data to NumPy

    Other libraries often expect a plain array rather than a labeled pandas object. When another library needs an array, to_numpy() returns a NumPy representation of the values.

    The conversion is deliberate, because it changes what the data is. A DataFrame conversion to NumPy omits row and column labels: the numbers survive, the coordinate system does not.

    16 / 20
  18. Handing the data to NumPy

    There is a second effect, and it is easy to miss. Because a NumPy array has one dtype for the entire array, heterogeneous DataFrame columns may be converted to a common NumPy dtype. A table of dates, text, and decimals cannot keep three types inside one array, so pandas finds a type that can hold them all.

    Before After to_numpy()
    Row and column labels dropped
    One type per column one type for the whole array
    Values preserved

    Use the conversion deliberately: it changes the representation and removes the labels that pandas uses to express meaning and alignment. Convert at the edge of the analysis, where a library demands it — not in the middle, where the labels are still doing work.

    17 / 20
  19. Quick check

    A downstream function needs a NumPy array, but your analysis also depends on the customer labels and on the per-column types. What is the trade-off in converting a mixed DataFrame?

    1. AThe array keeps a separate type for each column and preserves every one of the pandas labels

      A NumPy array has one type for the whole array, and the conversion does not carry labels.

    2. BThe DataFrame collapses into one Series while keeping both of its labeled axes

      Conversion produces an array, not a Series, and the labeled axes are exactly what is lost.

    3. CThe array may fall back to a single common type, and both label axes are dropped

      Right. Mixed columns may be coerced to a common type, and the row and column labels are omitted.

    18 / 20

  20. Key takeaways

    • Series means one labeled dimension with one dtype, and those labels are its Index.
    • DataFrame means labeled rows and columns whose columns may have different dtypes — a table, a spreadsheet, a SQL result, or a dictionary of Series.
    • Index labels rows, columns labels columns, and shape reports the two axis lengths; dtypes reports one type per column, dtype the single type of a Series.
    • A row is one observation, a column is one variable, and selecting a column returns a Series that keeps the row Index.
    • Converting a DataFrame to NumPy removes its row and column labels, and mixed columns may be coerced to one common type.
    19 / 20
  21. Quick check

    Which summary of the vocabulary is correct?

    1. AIndex labels the rows, `columns` labels the columns, and `shape` reports the two axis lengths

      Right. Those three names cover row labels, column labels, and the size of the object.

    2. B`shape` labels the rows, Index reports each column's type, and `columns` counts the stored values

      `shape` reports lengths rather than labels, and the Index holds row labels rather than types.

    3. C`dtypes` supplies the labels of both axes, while `shape` lists the type held by every column

      `dtypes` reports column types, and `shape` reports the two axis lengths.

    20 / 20

  22. 7 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.