7 stages2 sections22 units

01.08 Lesson 8 of 10

48 min

Pandas: Load, Inspect & Select

Load a CSV into a DataFrame, inspect its schema and select the rows and columns you need.

By the end of this lesson

You will be able to

  • Load a CSV with read_csv.
  • Inspect rows, columns, dtypes and summary information.
  • Select columns and filter rows.
  • Create a derived column using vectorized expressions.

Load first, inspect before changing

Use read_csv to load the file, then inspect the first and last rows, shape, dtypes and missing-value counts. Do not begin cleaning before you understand what each column represents.

import pandas as pd

df = pd.read_csv("data/hour.csv")
print(df.head())
print(df.shape)
print(df.dtypes)
print(df.isna().sum())

Select columns and rows explicitly

Use bracket selection for columns, .loc for label-based selection and .iloc for position-based selection. Boolean masks make a filter readable. Prefer these explicit operations over chained indexing.

hour_and_count = df[["hr", "cnt"]]
working_hours = df.loc[df["workingday"] == 1, ["hr", "cnt"]]
first_five = df.iloc[:5]

df["total_check"] = df["casual"] + df["registered"]

Apply what you learned

Exercise

Inspect the Bike Sharing CSV

Load hour.csv and produce a compact first-look report before cleaning anything.

  1. Read the CSV with pandas.
  2. Record the exact row and column counts from your downloaded file.
  3. Print the schema and the first five rows.
  4. Select hr, temp, hum and cnt.
  5. Filter working-day rows and compare their row count with the full dataset.
Done when

Your report distinguishes file facts from assumptions and includes the downloaded file name and a schema snapshot.

Free references

Study resources

2 links
  1. 01
    Getting started tutorials Article

    Use the tutorials for reading/writing data, selecting subsets and creating derived columns.

  2. 02
    Getting Started with Data Analysis Video

    Watch this single video for loading a CSV and inspecting its DataFrame.

Check your understanding

Self-test

2 questions
  1. Question 1

    What does df.shape return?

  2. Question 2

    When would you use .loc instead of .iloc?

Completion check

Move on when

You can load a CSV, inspect its schema and make explicit selections and filters.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels