Get startedGet started for free

Packages

1. Packages

Python's built-in modules are extensive, but if they don't offer what we need, we have another option - packages.

2. Modules are Python files

Remember that a module is a single Python file. While modules come built into Python, anyone can create their own!

3. Packages

Many developers have done exactly this, creating collections of modules known as packages, also called libraries. Packages are publicly available and free of charge! To access a package, we first need to download it from the Python Package Index, known as PyPI, which is essentially a directory of packages. Afterwards, we can import the package using the same approach we took with modules.

4. Installing a package

To download a package from PyPI, we open a terminal in macOS or the Command Prompt in Windows. A terminal is a text-based interface for running commands in our computer. Once opened, we type `python3 -m pip install`, followed by the package name. Python3 executes Python code from the terminal, while pip stands for the preferred installer program, a tool used to install Python packages.

5. Installing a package

Let's look at pandas, a popular package for data manipulation and analysis! As a developer, you'll often work with CSV files or database exports - pandas makes these tasks incredibly efficient. Let's install pandas first to access it in our script.

6. Importing with an alias

We can now use pandas by importing it, just like we did with the `os` module. However, it's common to use an alias when importing packages. To assign an alias, we import pandas with the `as` keyword followed by our alias name. The convention is `pd`, helping us shorten our code.

7. Creating a DataFrame

Say we have a dictionary containing sales data with `user_id` and `order_value` keys, each containing lists with respective values. We can use pandas to turn this into tabular data, similar to what we see in a spreadsheet. To do this, we use the pandas `DataFrame()` function, writing `pd.DataFrame()` because we imported pandas under the alias `pd`. Outputting the variable shows the data is now organized as a table, with each row showing the user_id and their order value.

8. Reading in a CSV file

We might have our data saved in a CSV file and want to read it into Python as a pandas DataFrame. To do this, we call the `pd.read_csv()` function and pass the file name as a string inside the parentheses. Checking the type confirms it is a pandas DataFrame.

9. Previewing the file

If there are many rows in our data, we might not want to look at all of them. pandas DataFrames have a method for this - we call `.head()`, and the first five rows are displayed!

10. Checking the file info

If we want to quickly overview the data, the `.info()` method comes in handy. The output shows the number of values, column types, memory usage, and much more.

11. Functions versus methods

We've discussed functions and methods, but let's clarify how they differ. A function is called to perform a task. A method is a function that is specific to a data type. Function examples include the built-in `sum()` function or the pandas `DataFrame()` function, where we use the syntax of package name dot function. For methods, `.head()` is a method specific to a data type - in this case, a pandas DataFrame. Therefore, it won't work if we try to use it with other data types like lists.

12. Let's practice!

Time to practice working with packages!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.