Now that we've discussed the importance of NumPy and Pandas and the options for setting up your environment, it's time to put theory into practice. This hands-on exercise will guide you through installing the libraries (if you haven't already) and verifying that everything is working correctly by running some basic code in a Jupyter Notebook.
Depending on whether you chose to use Anaconda or pip
directly, the steps for installation differ slightly.
Anaconda simplifies package management. If you installed Anaconda as recommended in the "Setting Up Your Environment" section, you likely already have NumPy, Pandas, and Jupyter installed. However, let's confirm or install them explicitly.
Open Anaconda Prompt (or Terminal on macOS/Linux):
Install/Update Libraries: It's good practice to ensure you have up-to-date versions. Execute the following command:
conda install numpy pandas jupyterlab
Conda will check if these packages are installed, update them if necessary, or install them if they are missing. It will also handle installing any required dependencies automatically. You might be prompted to confirm the installation plan; type y
and press Enter if so.
Verify Installation (Optional): You can list installed packages managed by Conda to check:
conda list numpy pandas
This command should display the installed versions of NumPy and Pandas.
If you are managing your Python environment directly using pip
(Python's standard package installer), follow these steps.
Open Terminal or Command Prompt:
Install Libraries: Use pip
to install NumPy, Pandas, and JupyterLab (which includes Jupyter Notebook):
pip install numpy pandas jupyterlab
Note: Depending on your system configuration, you might need to use pip3
instead of pip
, especially if you have both Python 2 and Python 3 installed. On some systems, particularly Linux and macOS, using python -m pip install ...
is a more robust way to ensure you're using the pip
associated with your intended Python interpreter.
Verify Installation (Optional): You can use pip
to show details about the installed packages:
pip show numpy pandas
This command will display information about the NumPy and Pandas packages if they were installed successfully.
With the libraries installed, let's start JupyterLab, the interactive environment we'll use throughout this course.
cd
(change directory) command to navigate to the folder where you want to save your notebooks for this course. For example:
cd path/to/your/projects/essential-numpy-pandas
Replace path/to/your/projects/essential-numpy-pandas
with the actual path on your computer. Working within a specific project folder helps keep your files organized.jupyter lab
This command will start the JupyterLab server. Your default web browser should automatically open, displaying the JupyterLab interface. If it doesn't open automatically, the terminal will provide a URL (usually starting with http://localhost:8888/lab
) that you can copy and paste into your browser's address bar.Keep the terminal window running; closing it will shut down the Jupyter server.
Now, let's create a notebook and run some code to confirm NumPy and Pandas are ready.
Create a New Notebook: In the JupyterLab interface (which opened in your browser), look for the "Launcher" tab. Under "Notebook", click the "Python 3" kernel icon (it might have a slightly different name like "Python [conda env:base]" depending on your setup). This will create and open a new, untitled notebook file (.ipynb
).
Rename the Notebook (Optional but Recommended): Click on the "Untitled.ipynb" name at the top of the notebook area and rename it to something descriptive, like 01-Setup-Verification.ipynb
.
Enter and Run NumPy Code: In the first code cell (the box with [ ]:
next to it), type the following Python code:
import numpy as np
# Create a simple NumPy array
my_array = np.array([1, 2, 3, 4, 5])
# Print the array
print("My first NumPy array:")
print(my_array)
# Print the shape of the array
print("Array shape:")
print(my_array.shape)
To run the code in this cell, click inside the cell and press Shift + Enter
.
Verify NumPy Output: Below the cell, you should see the output:
My first NumPy array:
[1 2 3 4 5]
Array shape:
(5,)
Seeing this output confirms that NumPy is installed and working correctly. The import numpy as np
line imports the library, conventionally giving it the alias np
. We then created a simple 1-dimensional array and printed it, along with its shape (which is 5 elements along one dimension).
Enter and Run Pandas Code: A new code cell should appear below the output. If not, click the +
button in the notebook toolbar. In this new cell, type the following code:
import pandas as pd
# Create a simple Pandas Series
my_series = pd.Series({'a': 10, 'b': 20, 'c': 30})
# Print the Series
print("My first Pandas Series:")
print(my_series)
Press Shift + Enter
to run this cell.
Verify Pandas Output: You should see the following output:
My first Pandas Series:
a 10
b 20
c 30
dtype: int64
This output confirms that Pandas is also installed and operational. We imported it using the conventional alias pd
and created a basic Series (a one-dimensional labeled array) from a Python dictionary.
If you encountered errors during installation or when running the code:
conda
or pip
, it might mean Anaconda or Python wasn't added to your system's PATH environment variable during installation. Revisit the installation instructions for your operating system or try running the installation again, ensuring you select the option to add it to the PATH (if available and appropriate for your setup).No module named 'numpy'
or No module named 'pandas'
), the installation likely failed or you might be running the notebook using a different Python environment than the one where you installed the libraries. Ensure you are running jupyter lab
from the same environment (e.g., the same Anaconda Prompt/Terminal session) where you performed the installation. Try the installation command again.np.__version__
and pd.__version__
after importing them in a notebook cell.Successfully running these simple code snippets means your environment is correctly set up with NumPy and Pandas, and you're familiar with the basics of executing code in a Jupyter Notebook. You are now ready to proceed to the next chapters and start working with these powerful libraries. Remember to save your notebook (File -> Save Notebook or Ctrl+S/Cmd+S) and you can shut down the Jupyter server by going back to the terminal window where you ran jupyter lab
and pressing Ctrl + C
twice.
© 2025 ApX Machine Learning