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
Transcript

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.

Since it’s part of the Python Standard Library, you don’t need to install it with pip; you can import it immediately.

Suppose you have a list of objects, say, condiments, and you want to devise a list of all possible three-condiment combinations. If you import itertools, you can do that with a single call to its combinations() function!

Enter the following into a new code cell and run it:

import itertools

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

You’ll see a series of tuples, which together list all the possible combinations of three condiments from the list.

Note: A tuple is an ordered list of items. In the output from above, you’ll see that any tuple of 3 items that has ketchup in it has ketchup as the first item since tuples are ordered and ketchup is the first item in the condiments list.

Try another itertools function, this time importing it using the fromimport syntax so you can call it by name without referencing its module. This function is permutations(), which can produce all the possible ways a collection can be ordered:

from itertools import permutations

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

This produces a large list of tuples, each containing a different way to order’ condiments’. It’ll take up a lot of space in your notebook, but you can easily dismiss the output by selecting the code cell, opening JupyterLab’s Edit menu, and selecting Clear Cell Output.

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.

Pandas is one of Python’s most-used data science tools. Its primary data structure, DataFrame, is a spreadsheet-like construct that works well for importing large tables of data and then sorting, filtering, transforming, and performing calculations and analysis on them — and with an efficiency that you can’t get with Python’s own native lists and dictionaries. Use Pandas to set up a DataFrame of popular programming languages.

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.

Insert a new code cell below the Markdown cell and run the following in it:

! pip install pandas
! pip install matplotlib

Note: You’ll use matplotlib later in the lesson.

You may recall that any line in a code cell that begins with ! isn’t executed as Python code but as commands on the command line.

Running this command will cause pip to install the Pandas library on your system. Several status messages will appear below the code cell, and when they stop appearing, the installation is complete.

You can confirm that Pandas was installed in a couple of different ways. One way is to use pip show.

Run the following in a new code cell to confirm that Pandas was installed:

! pip show pandas

Now that Pandas has been installed, it’s time to put it to use. Import Pandas by running the following in a new code cell:

import pandas as pd

Pandas’ DataFrame data structure and data science methods are so useful to data science projects that they are called repeatedly, which means typing pandas. often. You’ll see that many developers use pd as an alias for pandas.

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,
  ],
})

This code defines a dataframe named languages_df. In many applications that use Pandas, you’ll see either a variable called df that refers to a single dataframe, or a set of variables for dataframes with names ending in _df.

The dataframe is built by giving its constructor a dictionary where each key is a column name, and each corresponding value is a list with that column’s contents. This dictionary passed to the constructor has these four keys:

  • language: The programming language.
  • sept_2024_ranking: The programming language’s rank in September 2024.
  • rating: The programming language’s share of searches compared to all programming language searches.
  • change: The programming language’s month-to-month change in rating, in percent.

You’ve probably noticed that the first element in the "language" list, which defines the column of programming languages, is wrong. That mistake is intentional and will be addressed soon.

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)

This displays a pure text version of the contents of the languages_df dataframe.

To see the developer-facing representation, type languages_df into a code cell and run it:

languages_df

Within a Jupyter notebook, the developer-facing representation of a DataFrame is a nicely rendered table with rows that highlight themselves when the cursor hovers over them.

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"

DataFrame’s at() method uses array notation to read or write to specific cells. The code above updates the cell at row 0 of the “language” column to "Python". Rerun the languages_df cell to see the updated value.

Now that the DataFrame’s data is correct, you can display it as a graph using DataFrame’s plot() function:

languages_df.plot(kind="bar", x="language", y="rating")

The code above specifies that the graph produced by plot() should be a bar graph and that its x-axis should be based on the column named "language" and the y-axis should be based on the column named "rating". Rerun the languages_df cell to see the updated value.

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

When you run it, pip will inform you that uninstalling Pandas would remove a couple of directories. It’ll ask, Proceed (Y/n)? — but there’s no way to answer!

Unfortunately, pip uninstall wasn’t designed with being run from within JupyterLab in mind. Stop the cell’s execution by clicking the Stop button near the top center of the JupyterLab window. Then switch to the command line, then enter:

pip uninstall pandas

Remember that you’re no longer inside JupyterLab and don’t have to precede shell commands with a !. This time, when you run the pip uninstall command, you can type Y in response as confirmation, and pip will uninstall Pandas.

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