liveBook · Manning

Chapter 4: Using a Coding Companion with Python

What you will learn in chapter 4 of Learn AI Data Engineering

The chapter introduces using an AI coding companion to build and refine Python solutions for repetitive data engineering tasks while keeping the engineer in control.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces using an AI coding companion to build and refine Python solutions for repetitive data engineering tasks while keeping the engineer in control.

Key terms: coding companion, few-shot prompting, iterative prompting, targeted prompts, API interactions, nested JSON, regular expressions

4.1 Interacting with APIs Using AI Coding Companions & Python

Introduces why data engineers use APIs, what makes API work tedious, and how AI coding companions can help with Python API interactions.

Key terms: APIs, HTTP requests, JSON responses, errors, AI coding companions, ChatGPT, boilerplate code, edge cases, API documentation

4.1.1 Fetching Data from an API

This section uses NumbersAPI to show how an AI coding companion can generate Python API code, then improve it through clearer prompts and iterative refinement.

Key terms: NumbersAPI, JSON, query parameters, zero-shot prompt, requests, pandas DataFrame, error handling, overengineering, prompt ambiguity, Jupyter notebook

4.1.2 Enhancing API Calls with AI Coding Companions and API Documentation

This section shows how to use uploaded API documentation to prompt an AI coding companion for Python API code, then verify the code against the documentation and the live API.

Key terms: API documentation, AI coding companion, upload, NumbersAPI, numbers_api_reference.txt, notfound=floor, fragment=true, JSON, pandas DataFrame, required parameters

4.2 Unnesting Complex JSON Objects with AI Companions & Python

Introduces AI-assisted flattening of nested API JSON into analysis-ready pandas DataFrames and the JSONPlaceholder dataset used to practice it.

Key terms: nested JSON, unnesting, AI coding companions, pandas DataFrames, JSONPlaceholder API, address, company, 10 user records

4.2.1 Simple Example: Flattening a Single Nested Field

A simple JSONPlaceholder example shows how a coding companion can flatten one nested user field into a pandas DataFrame.

Key terms: JSONPlaceholder, pandas DataFrame, address.city, flattening JSON, .get()

4.2.2 Complex Example: Extracting Deeply Nested & Combined Fields

This section shows how to use a coding companion to flatten deeply nested API data into a DataFrame while guarding against inconsistent JSON structures.

Key terms: few-shot prompting, JSONPlaceholder, DataFrame, company.name, company.catchPhrase, address.city, address.geo, geo.lat, geo.lng, Geo

4.3 Using AI to Implement Regex Patterns

This section introduces AI-assisted regex as a way to extract and normalize messy text, using Python phone-number examples that will be refined for edge cases.

Key terms: regular expressions, regex, AI coding companions, Python’s re module, extensions

4.3.1 Extracting Phone Numbers from Text

The section shows how a conversational prompt can produce Python regex code that extracts U.S. phone numbers in different formats from a block of text.

Key terms: Python script, U.S. phone numbers, regex

4.3.2 Normalizing Phone Numbers with Regex and AI

This section uses an AI coding companion to turn extracted phone numbers into a consistent format.

Key terms: normalize, few-shot, non-digit characters, +1-XXX-XXX-XXXX

4.3.3 Extracting Number Components into a DataFrame

This section uses AI-generated regex to split U.S. phone numbers into structured fields, compares DataFrame and JSON output, and warns that regex must be tested against messy inputs.

Key terms: capturing groups, area_code, first_3, last_4, pandas DataFrame, regex, test cases, real-world samples, phonenumbers, dateutil

4.4 Lab

This lab asks readers to use an AI coding companion and Jupyter Notebook to retrieve brewery data, clean phone numbers and website URLs with regex, and verify the results.

4.5 Lab Answers

The section gives three lab-answer prompts for fetching brewery data into a pandas DataFrame and using regex to format phone numbers and extract website domains.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Using a Coding Companion with SQL Chapter 5: Using the OpenAI API in Data Workflows →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.