JupyterLab, a powerful interactive environment for Python, allows you to seamlessly work with various libraries to enhance your data analysis, visualization, and machine learning projects. This guide will equip you with the essential knowledge on how to import libraries and get started with using them in your JupyterLab notebooks.
Understanding Libraries
Libraries (or packages) are collections of pre-written Python code that provide functionalities for specific tasks. They save you time by offering well-tested, efficient solutions to common programming problems.
Examples of popular libraries in data science include NumPy (numerical computing), pandas (data manipulation), matplotlib (visualization), and scikit-learn (machine learning).
Pre-installed Libraries
JupyterLab typically comes with a set of core libraries already installed. You can check these by running the following code in a code cell:
import sys
print(sys.modules)
This will list all the built-in and installed modules available in your environment.
Installing Additional Libraries
If you need libraries beyond the pre-installed ones, you can install them directly within JupyterLab using the !pip install command:
!pip install pandas numpy
Importing Libraries
Once a library is installed, you can import it into your notebook using the import statement:
import pandas as pd
import numpy as np
As a convention, many users give imported libraries shorter aliases for easier use. For example, pd for pandas and np for numpy.
Using Library Functions and Classes
After importing a library, you can access its functions and classes to perform various tasks. Refer to the library's documentation for comprehensive details on the provided functionalities.
Example with NumPy and pandas:
import numpy as np
import pandas as pd
# Create a NumPy array
arr = np.array([1, 2, 3, 4])
print(arr)
print(\n)
# Create a pandas DataFrame
data = {'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']}
df = pd.DataFrame(data)
print(df)
The best way to understand and learn how to perform this function is through hands-on experience. Follow the steps below to create the sample notebook in your Syntasa environment:
- Download the sample notebook .ipynb file from this article.
- Create a new notebook in your Syntasa environment using the import notebook option.