PART I
(i)Provide the standard, idiomatic Python code for loading the NumPy library. Briefly
describe the main purpose of NumPy in computational science and engineering.
Loading NumPy and its Purpose
Standard idiomatic Python code for loading the NumPy library:
import numpy as np
NumPy (Numerical Python) is a core Python library used for numerical and scientific
computing. It provides efficient N-dimensional array objects, fast vectorized operations,
and tools for linear algebra, Fourier transforms, and statistical analysis. In civil and
structural engineering, NumPy is applied in handling large datasets, matrix operations in
finite element analysis, and numerical modeling of engineering systems.
(j)Given a NumPy array A=[Link]([10,20,30,40,50]), write the Python expression(s)
required to perform the following:
i. Retrieve the first element (10) using its index.
ii. Slice the array to return the sub-array [20,30,40].
Array Operations using NumPy
Given array: A = [Link]([10, 20, 30, 40, 50])
i. Retrieving the first element (10) using its index:
A[0]
ii. Slicing the array to return the sub-array [20, 30, 40]:
A[1:4]
Python uses zero-based indexing, so A[0] returns the first element. The slice A[1:4]
returns elements from index 1 up to, but not including, index 4.
(i)What is the purpose of the following two Pandas DataFramemethods in data cleaning?
i. .dropna() ii. .drop(0)(when applied to a DataFrame where the first row is an index of 0)
Purpose of Pandas DataFrame Methods in Data Cleaning
i. .dropna():
Removes all rows (or columns) that contain missing (NaN) values. This method is used
to clean datasets by eliminating incomplete data entries, ensuring accurate analysis and
computations.
ii. .drop(0):
Removes the row or column with the specified label—in this case, the row with index 0.
It is used to delete the first row, often containing invalid or redundant information. This
method is used in data cleaning to eliminate specific rows, such as an unnecessary or
erroneous first row, based on its index label.