Module 3 Unit 2a - Working with Python Libraries

Callysto.ca Banner

Module 3 Unit 2a - Working with Python Libraries#

Libraries are one of the reasons Python is such a versatile and powerful language. Libraries are packages of code developed by others that let us add new and interesting tools to our projects.

There are literally thousands of Python libraries available for use—including The Python Standard Library and some popular, specialized libraries such as Numerical Python (NumPy) or the Python data analysis module (pandas). There are several ways to make these libraries available in our Jupyter notebooks. In this optional unit, we’ll explore how to

  1. Import individual functions

  2. Import entire libraries

  3. Import libraries with an alias

pandas

Importing a few functions at a time#

In this module we make extensive use of the Python data analysis library, pandas.

In many of our demonstrations and tutorials, we import just one function at a time and use it immediately.

For example, this code makes the read_csv tool from the pandas library, then uses that code to create a DataFrame from a spreadsheet file.

from pandas import read_csv
colours_df = read_csv('https://tinyurl.com/y73h8nre-colours')
colours_df

We could also import several functions at once, as in the following:

from pandas import Series, DataFrame, read_csv
my_series = Series([1,2,4,5,6])
my_df = DataFrame(my_series)
colours_df = read_csv('https://tinyurl.com/y73h8nre-colours')

Importing a full library#

The asterix (*) notation lets us import all functions in a library at once.

For example:

from pandas import *
my_series = Series([1,2,4,5,6])
my_df = DataFrame(my_series)
colours_df = read_csv('https://tinyurl.com/y73h8nre-colours')

The advantage of this method is specifying the exact functions we need from the library and using these functions directly with their simple names.

However, a problem arises if we are using multiple libraries, which might use the same name for different things.

For example, there are several different libraries that include a plot function.

Fortunately, Python has a simple solution that allows us to specify which one we are using in our commands.

With a slightly different syntax, we can import an entire library, and include the name of the library whenever we call a particular function.

For instance, the Series function from the pandas library is accessed using the command pandas.Series

from pandas import Seriesimport pandas
my_series = pandas.Series([1,2,4,5,6])
my_df = pandas.DataFrame(my_series)
colours_df = read_csv('https://tinyurl.com/y73h8nre-colours')

With this method our commands are a bit longer, but allow us to keep our libraries organized and comprehensive.

Importing a library with an alias#

Another method for specifying the library of a function is with aliases.

An alias is a short, unique name we give a library we’ve imported to keep track of where our functions are coming from. Aliases also help reduce the amount of typing we have to do when inputting commands.

For instance, we can import the pandas library with the shortened name pd. When we call a function from the pandas library, like Series, we refer to it as pd.Series in our code.

import pandas as pdimport pandas as pd
import numpy as np
import matplotlib.pyplot as plt

my_data = np.array([1,2,4,5,6])
my_series = pd.Series(my_data)
plt.plot(my_series)

In this example, the plot function is coming from the matplotlib library, which is important to know since the pandas library also has its own version of plotting functions.

Aliases can help people who are reading our code understand how it works. They can also help the Jupyter notebook keep track of functions from different libraries that might have the same or similar names.

Callysto.ca License