Data Science · Intermediate level

Intermediate level · Module 2: Data Wrangling with pandas and NumPy · Lesson 3 of 3

Vectorised Operations, Dates and Text

Transform data efficiently with vectorised operations and handle dates and text correctly.

In this lesson you will

  • Replace loops with vectorised pandas and NumPy operations
  • Parse and use dates and clean text columns

Key concepts

vectorisationnp.wherepd.to_datetime.str methods

The lesson

Vectorised operations work on whole columns at once: df["rate"] = df["present"] / df["enrolled"]. They are shorter and far faster than looping over rows.

np.where and np.select create columns from conditions, for example classifying schools as low, medium or high attendance.

pd.to_datetime converts text to dates, after which you can extract months or calculate durations. The .str methods clean text: .str.strip(), .str.title(), .str.replace().

Step by step

  1. Calculate attendance rates with a vectorised expression.
  2. Classify schools with np.select.
  3. Parse dates and extract the term.
  4. Standardise school names with .str methods.

Practice activity

Complete the analysis table with derived indicators, terms and clean names, and write the data dictionary.

Expected result: An analysis-ready table and a data dictionary describing every column.

Check your understanding

  1. Why prefer vectorised operations over loops?

    Show answer

    They are shorter and much faster.

  2. Which function converts text to dates in pandas?

    Show answer

    pd.to_datetime.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.