Now that you have set up your environment and have a basic understanding of Jupyter Notebooks, it's time to write and execute your first few lines of code using NumPy and Pandas. This step is essential to confirm that the libraries are installed correctly and to get a feel for how they operate.
The first step in using any Python library is to import
it into your current script or notebook session. This makes the library's functions and objects available for you to use. For NumPy and Pandas, there are standard community conventions for importing them:
np
.pd
.Using these aliases makes your code shorter and more readable, as they are widely recognized.
Open your Jupyter Notebook (or Python interpreter) and type the following code into a cell:
# Import the NumPy library
import numpy as np
# Import the Pandas library
import pandas as pd
# Print a confirmation message
print("NumPy and Pandas imported successfully!")
print(f"NumPy version: {np.__version__}")
print(f"Pandas version: {pd.__version__}")
To run this code in Jupyter, press Shift + Enter
. If the installation was successful, you should see the confirmation message printed below the cell, along with the installed versions of NumPy and Pandas. If you encounter an error message like ModuleNotFoundError
, double-check the installation steps from the previous section ("Setting Up Your Environment").
NumPy's primary object is the N-dimensional array, often called an ndarray
. Let's create a simple one-dimensional array from a standard Python list.
# Create a Python list
my_list = [1, 2, 3, 4, 5]
# Create a NumPy array from the list
np_array = np.array(my_list)
# Print the array
print("My first NumPy array:")
print(np_array)
# Print the type of the array object
print("Type of the object:")
print(type(np_array))
Running this cell will output:
My first NumPy array:
[1 2 3 4 5]
Type of the object:
<class 'numpy.ndarray'>
Notice how the output [1 2 3 4 5]
looks slightly different from a Python list [1, 2, 3, 4, 5]
. This indicates it's a NumPy array. The type()
function confirms that np_array
is indeed an instance of the numpy.ndarray
class.
NumPy arrays allow for efficient element-wise operations. Let's try a simple one: multiplying every element by 2.
# Perform an element-wise operation
doubled_array = np_array * 2
print("Array after multiplication:")
print(doubled_array)
Output:
Array after multiplication:
[ 2 4 6 8 10]
With a standard Python list, you would need to use a loop or list comprehension to achieve this. NumPy handles this much more efficiently behind the scenes.
Pandas provides two main data structures: Series
(1-dimensional) and DataFrame
(2-dimensional).
Let's create a Series
. A Series is like a one-dimensional NumPy array but with an associated index.
# Create a Pandas Series from a list
data_series = [10, 20, 30, 40, 50]
pd_series = pd.Series(data_series)
# Print the Series
print("My first Pandas Series:")
print(pd_series)
# Print the type of the Series object
print("\nType of the object:")
print(type(pd_series))
Running this cell will output:
My first Pandas Series:
0 10
1 20
2 30
3 40
4 50
dtype: int64
Type of the object:
<class 'pandas.core.series.Series'>
Observe that the output includes both the values (10, 20, etc.) and an index (0, 1, 2, etc.) on the left. The dtype: int64
indicates the data type of the values stored in the Series.
Now, let's create a DataFrame
. A DataFrame is a two-dimensional, table-like structure where columns can have different data types. A common way to create one is from a Python dictionary, where keys become column names and values (lists or arrays) become the column data.
# Create a dictionary of data
data_dict = {
'StudentID': [101, 102, 103, 104],
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Score': [85, 92, 78, 88]
}
# Create a Pandas DataFrame from the dictionary
df = pd.DataFrame(data_dict)
# Print the DataFrame
print("My first Pandas DataFrame:")
print(df)
# Print the type of the DataFrame object
print("\nType of the object:")
print(type(df))
Running this cell produces:
My first Pandas DataFrame:
StudentID Name Score
0 101 Alice 85
1 102 Bob 92
2 103 Charlie 78
3 104 David 88
Type of the object:
<class 'pandas.core.frame.DataFrame'>
This output clearly resembles a table with labeled columns ('StudentID', 'Name', 'Score') and an automatically generated row index (0, 1, 2, 3).
Executing these simple code snippets confirms that your environment is ready and gives you a first glimpse of creating and interacting with the fundamental data structures in NumPy and Pandas. In the following chapters, we will build upon these basics, exploring the capabilities of these libraries in much greater detail.
© 2025 ApX Machine Learning