1. Lighthouse

Level 6 · Load the supplier's CSV and check its types

The request

From Priya Shah, Head of Data

“A supplier pasted their delivery note into an email as CSV text. Load it into a DataFrame and tell me what type pandas gives each column. I want to know what we're dealing with before anyone sums anything.”

Your goal

Load csv_text with pd.read_csv(io.StringIO(csv_text), parse_dates=["received_on"]). Set result to a dictionary mapping each column name to its dtype as a string, as returned by df.dtypes.astype(str).to_dict().

  • Store a dict in resultnot done yet

Your code

Python

Loading the editor…

    ResultPress Run to see yours

    Mote checks your result

    Press Run. Each row that matches the goal becomes a firefly in your lantern.

    The lesson

    Reading CSV text with read_csv

    pd.read_csv is how almost every analysis starts. Usually you give it a path, like pd.read_csv("orders.csv"). The Harbor & Pine tables in this game were loaded exactly that way, which is why orders and products are ready for you.

    Sometimes the CSV isn’t a file: it’s text pasted into an email, returned by an API or typed into a test. io.StringIO wraps a string so that it behaves like an open file, and read_csv can’t tell the difference:

    import io
    
    df = pd.read_csv(io.StringIO(csv_text), parse_dates=["received_on"])
    df.shape    # (4, 5)

    The first line becomes the column names. parse_dates tells pandas which columns hold dates, so they become real datetimes instead of text. Without it, received_on would come back as object, and sorting or subtracting dates would not work.

    What dtypes tells you

    df.dtypes lists the type pandas chose for each column. On this delivery note:

    df.dtypes
    # sku                    object
    # product                object
    # units                 float64
    # unit_cost             float64
    # received_on    datetime64[ns]

    Text is stored as object. Numbers with decimals are float64, whole numbers are int64, and parsed dates are datetime64[ns].

    Why units became float64

    Every quantity in the file is a whole number, yet units is float64. The throw’s quantity is blank. pandas marks a missing value as NaN (“not a number”), and NaN only exists as a float, so the whole column is stored as floats: 40 becomes 40.0.

    You’ll see the same thing in the store data. In employees, manager_id is float64, not int64, because the CEO has no manager and that cell is NaN. df.info() shows it too: the Non-Null Count of units is 3 out of 4, which points straight at the gap.

    Common mistakes

    • Passing the text itself to read_csv. Without io.StringIO, pandas treats the text as a file name and fails with a FileNotFoundError.
    • Forgetting parse_dates, so dates stay as text.
    • Treating a float64 id or count column as a bug. It usually means there is at least one missing value; find it with .info() before converting.

    Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.