Prepstellar

Data Analysis Fundamentals · Getting started

22 cards

Pandas for Tabular Analysis

Swipe, scroll or use ← →
  1. Two objects, two shapes

    Before writing a single calculation, decide which object will hold the data. That choice decides what pandas can do with it afterwards, so it comes first.

    Pandas provides two main classes for handling labeled data in Python. A Series holds one-dimensional labeled values. A DataFrame holds two-dimensional data arranged as rows and columns. This makes a DataFrame a natural representation for the tabular shape that appears in a spreadsheet or a database result.

    Object Shape What it fits
    Series one labeled axis one measured sequence, such as a daily reading
    DataFrame rows and columns a table whose rows are observations and whose columns are variables

    Two more words come up constantly, and neither of them is a container for a whole table. An Index supplies the labels of an axis, and a scalar is a single value.

    1 / 22
  2. Two objects, two shapes

    A real table rarely holds one kind of value. Columns in one DataFrame can have different data types, so numbers, text, dates, and categories can coexist while retaining their column identity.

    order_id customer ordered_on total status
    1001 Ana Ruiz 2024-03-01 249.50 shipped
    1002 Beca Lim 2024-03-02 89.00 pending
    1003 Cai Ono 2024-03-02 15.75 shipped

    Five columns, five kinds of value: whole numbers, text, dates, decimals, and a short list of repeated categories. A DataFrame with observations as rows and variables as columns therefore preserves both the shape of the table and the types of the variables inside it. One labeled dimension could not do that, because a Series carries a single sequence rather than several named variables with separate types.

    2 / 22
  3. Quick check

    A dataset has one row per order, four named variables, and a different type in each variable. Which object preserves both the table shape and the variable types?

    1. AA Series, since one labeled dimension holds every named variable at once and keeps each type

      A Series has one labeled dimension and one kind of value, so it cannot keep four named variables with separate types.

    2. BA DataFrame, with orders as rows and variables as columns

      Right. A DataFrame gives two labeled axes, and its columns may each keep their own data type.

    3. CAn Index built from the observations, the variables, and the values together

      An Index supplies the labels of an axis; it is not the store that holds a table's values.

    3 / 22

  4. Bring pandas in and pick the shape

    Pandas works inside Python and is commonly imported with import pandas as pd. The alias is a convention rather than a rule of the language, but it is used so consistently that pd. reads as "from pandas" to anyone who works with the library.

    The statement does two things and no more: it loads the package, and it binds the short name. It does not import a Series or a DataFrame separately, there is no load keyword that brings pandas into an Index, and reversing the order of the words produces an error rather than a shorter alias.

    4 / 22
  5. Bring pandas in and pick the shape

    With pandas loaded, the shape of the data decides the object. Use a Series when one labeled sequence is the object of interest. Use a DataFrame when several variables belong together in a table with labeled rows and columns.

    Data you have Object Labels
    One temperature per day for a month Series the day of each reading
    Temperature, humidity, and wind per day DataFrame days as rows, measures as columns
    One country's population in a single year scalar none; it is a lone value

    In both pandas cases, retain the labels when they carry business or analytical meaning, because everything that follows depends on them.

    5 / 22
  6. Quick check

    You have one temperature reading per day for a month and nothing else. Which setup matches that data?

    1. A`import pandas as pd`, then a Series indexed by the day labels

      Right. The conventional import binds the short name pd, and one labeled sequence of values is exactly the shape of a Series.

    2. B`import pandas as pd`, then an Index whose entries are the readings themselves

      The import line is correct, but an Index carries the axis labels rather than the measured values.

    3. C`load pandas into Index`, then one repeated value standing for the month

      That is not a valid import statement, and a single repeated value would erase the day-by-day readings.

    6 / 22

  7. What labels add

    A raw array can be treated as a row of anonymous positions: the third value is simply the third value. Pandas takes a different position. Its labels attach meaning to values through an Index and through column names, so the third value is "the reading for March" and stays that even if the rows move.

    The link between labels and data is intrinsic and is not broken unless you explicitly change it. Sort the rows, filter them, or select a subset, and the labels travel with their values.

    7 / 22
  8. What labels add

    That is what "identity beyond numeric position" means in practice.

    What identifies a value Survives sorting? Survives filtering?
    Its label in the Index yes yes
    Its position in the printed output no no
    Its position after conversion to a plain array no the labels are gone anyway

    Display order is a consequence of the last operation, not a property of the data. The label is the property of the data, and it is what later calculations use.

    8 / 22
  9. Quick check

    Two analysts describe the same Series. Which description explains what gives one of its values a lasting identity?

    1. AIts place in the printed output, recomputed after every operation you run

      Display order changes with sorting and filtering, so it cannot be what identifies a value.

    2. BIts conversion into a plain array, with the labels stripped off first

      Removing labels is what takes identity away; it cannot be what supplies it.

    3. CIts label in the Index, which stays attached to the value

      Right. The Index label stays attached to its value, and that link is broken only if you change it deliberately.

    9 / 22

  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. Operations follow labels

    Labels would be decoration if calculations ignored them. They do not: operations between Series automatically align data by label. Adding two Series does not pair the first row with the first row. It pairs the value labeled Jan with the value labeled Jan.

    That is why row order stops being a source of silent error. Two extracts of the same measure can arrive sorted differently, and the arithmetic still matches like with like.

    10 / 22
  12. Operations follow labels

    Two monthly measures, stored in different orders:

    Series A value Series B value
    Feb 20 Jan 5
    Jan 10 Feb 7

    Adding them gives Jan 15 and Feb 27. A positional calculation would have combined January with February and reported 17 and 17: two numbers that look plausible and mean nothing. Nothing in the output would have flagged the mistake.

    11 / 22
  13. Quick check

    Two Series hold the same three regions, but their rows are in different orders. What happens when you add them?

    1. AEach region is added to its own label, whatever the row order

      Right. Series arithmetic aligns on the Index, so the row order does not decide which observations meet.

    2. BThe top row of one is added to the top row of the other

      That is positional pairing, which is exactly what label alignment avoids.

    3. CPandas refuses the addition until both are sorted the same way

      No matching order is required; alignment handles the difference for you.

    12 / 22

  14. The union of labels, and what missing means

    Aligned inputs do not always cover the same labels. When two Series have different indexes, the result uses the union of those indexes; a label missing from either input receives a missing result.

    The union is a deliberate choice. Keeping only the labels present in both inputs would quietly delete observations, and a report built on that result would be short of rows without saying so. The union keeps every label and marks the gaps.

    13 / 22
  15. The union of labels, and what missing means

    Suppose one input covers January to March and the other only January and February:

    Label Input A Input B Sum
    Jan 10 5 15
    Feb 20 7 27
    Mar 30 missing

    A missing result at one label after Series addition means that label was absent from one of the inputs. It does not mean the two inputs agreed, it does not mean pandas fell back to positions, and it does not mean the labels were replaced by a numbered range. Deciding what the gap means — a late arrival, a closed store, a broken feed — is analytical work, and the missing marker is what puts that decision in front of you.

    14 / 22
  16. Quick check

    You add two Series and the result shows a missing value at the label Mar. What does that tell you?

    1. ABoth inputs held the same observation under that label

      Matching values would be added together and produce a number, not a gap.

    2. BOne of the two inputs simply had no value stored at that label

      Right. Alignment takes the union of the labels and marks any label that one input lacks.

    3. CThe two indexes were replaced by a plain numbered range

      Alignment keeps the labels; it does not renumber them.

    15 / 22

  17. A mismatch, worked end to end

    Put the two rules together on a case an analyst meets constantly. Two regional measures come from different systems. The row order differs, and one region appears in only one of the files. The comparison must be correct, and the gap must stay visible.

    Positional pairing fails the first requirement. Converting both to plain arrays fails the second, because the region names would be gone before the comparison started. Labeled Series aligned over the union of the region labels satisfies both.

    16 / 22
  18. A mismatch, worked end to end

    The same reasoning holds for the monthly case: two Series whose rows arrive in different orders, one of them without March.

    1. Alignment pairs each month with the same month, so the ordering is irrelevant.
    2. The union keeps Mar in the result.
    3. Mar is reported as missing, because one input has no value for it.

    The analyst still has to decide what the missing March means. What pandas guarantees is that the association between a value and its label stays explicit, so the decision is a decision rather than an accident.

    17 / 22
  19. Quick check

    Two regional measures arrive in different row orders, and one region appears in only one of them. You must compare them and keep that gap visible. What do you rely on?

    1. APositional pairing, which lines up the top rows and drops the extra region

      Positional pairing ignores the labels, so it can compare the wrong regions and lose the extra one.

    2. BConversion to plain arrays, which keeps the region names attached to the numbers

      Plain arrays have no labels, so the region names would be gone before the comparison began.

    3. CLabel alignment over the union of regions, which reports the gap as missing

      Right. Alignment matches regions despite the order, and the union keeps the unmatched region with a missing result.

    18 / 22

  20. Where this sits in the rest of the course

    Pandas is not a single command that turns a file into a finished answer. It is a set of operations over labeled objects, and the package supplies the labeled objects and operations used throughout this course: creating or importing data, inspecting it, selecting subsets, handling missing values, calculating results, combining tables, reshaping, working with time, plotting, and exporting.

    Later blocks teach each of those in depth. They all assume the two decisions made here: the shape of the object, and the labels it keeps.

    19 / 22
  21. Quick check

    Which sentence describes what the rest of this course builds on?

    1. AOne command that converts any file directly into a finished report

      Pandas is a toolkit of steps, not a single command that produces a finished answer.

    2. BA set of operations over labeled objects, from loading through to exporting

      Right. Loading, inspecting, selecting, cleaning, calculating, combining, reshaping, time work, plotting, and exporting all act on labeled objects.

    3. CA drawing library in which labels are decoration rather than structure

      Plotting is only one of the operations, and labels carry the structure the other steps depend on.

    20 / 22

  22. Key takeaways

    • A Series is one-dimensional; a DataFrame is a two-dimensional table with rows and columns.
    • Pandas labels give values identity beyond their numeric position, and the link between a label and its value is not broken unless you break it.
    • Series operations align by label and expose unmatched labels as missing results, over the union of the two indexes.
    • Columns in one DataFrame keep their own data types, so a table of mixed variables stays intact.
    • Load the library with import pandas as pd, then choose the shape: a Series for one labeled sequence, a DataFrame for a table of variables.
    21 / 22
  23. Quick check

    Which summary of this lesson is correct?

    1. AA Series has one dimension, a DataFrame has rows and columns, and operations pair values by label

      Right. Those are the two shapes and the alignment rule that everything later depends on.

    2. BA Series has two dimensions, a DataFrame has one, and pandas pairs values by row position

      The dimensions are the wrong way round, and pairing follows labels rather than positions.

    3. CBoth objects discard their labels before arithmetic, and any unmatched label is dropped

      Labels are kept through arithmetic, and unmatched labels stay in the union as missing results.

    22 / 22

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