Python for AI: A Crash Course

Nov 16 2024 · Python 3.12, JupyterLab 4.2.4

Lesson 03: Working with Python Libraries & Package Management

Python Libraries Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Demo

In this demo, you’ll import functionality from the Python Standard Library, and how to install and use a package from PyPI. Start Jupyter Lab and open the file, import-install-libraries-starter.ipynb.

Importing From the Python Standard Library

Begin the exercise by importing the itertools module from the Python Standard Library—partly for the importing practice and partly so you know it exists. Someday, when you least expect it, it’ll have the exact function you need.

import itertools

condiments = [
  "ketchup",
  "yellow mustard",
  "honey mustard",
  "dijon mustard",
  "relish",
  "mayonnaise",
  "barbecue sauce"
]
for combination in itertools.combinations(condiments, 3):
  print(combination)
from itertools import permutations

for permutation in permutations(condiments):
  print(permutation)

Importing Third-Party Libraries With pip

While the Python Standard Library has some valuable modules and packages and is worth exploring, many of the more interesting libraries are third-party ones.

Install and Import Pandas

Scroll to the Markdown cell with the heading Build a Pandas Dataframe. Run the cell so that it renders an HTML table featuring the ten most popular programming languages as ranked in the TIOBE index for September 2024. This is the table to be built in Pandas.

! pip install pandas
! pip install matplotlib
! pip show pandas
import pandas as pd

Build the DataFrame

You’ll see a code cell that starts with the comment # Build the dataframe. In it, you’ll see code that defines a DataFrame of programming languages. Run this cell:

# Build the dataframe

languages_df = pd.DataFrame({
  "language": [
    "COBOL",
    "C++",
    "Java",
    "C",
    "C#",
    "JavaScript",
    "Visual Basic",
    "Go",
    "SQL",
    "Fortran",
  ],
  "sept_2024_ranking": [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
  ],
  "rating": [
    20.17,
    10.75,
    9.45,
    8.89,
    6.08,
    3.92,
    2.7,
    2.35,
    1.94,
    1.78,
  ],
  "change": [
    6.01,
    0.09,
    -0.04,
    -2.38,
    -1.22,
    0.62,
    0.48,
    1.16,
    0.5,
    0.49,
  ],
})

Display the Dataframe’s Contents

DataFrame objects have two representations: a user-facing one and a developer-facing one. To see the user-facing one, use the print() function:

print(languages_df)
languages_df

Updating and Graphing the Dataframe’s Contents

The current version of the dataframe lists COBOL as the top language. Let’s fix that:

languages_df.at[0, "language"] = "Python"
languages_df.plot(kind="bar", x="language", y="rating")

Uninstalling a Package With pip

For completeness’ sake, you should try uninstalling Pandas even though you’ll probably reinstall it later. Do this by running the following in a new code cell:

! pip uninstall pandas
pip uninstall pandas
See forum comments
Cinema mode Download course materials from Github
Previous: Python Libraries Next: Virtual Environments