0% found this document useful (0 votes)
5 views

Lab10_PythonDataAnalysis

Uploaded by

seibouismael8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab10_PythonDataAnalysis

Uploaded by

seibouismael8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ENSA-Kenitra Module : Compétence Numériques

Lab 10: Python Data Analysis:


Introduction to Numpy and Pandas

Exercise 1: Exploring NumPy Basics

Objective: Understand basic operations with NumPy arrays.

1. Creating Arrays:
o Create a 1D NumPy array with the values [5, 10, 20, 30, 40, 50].
o Create a 2D NumPy array with the following values:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

2. Array Operations:
o Multiply each element in the 1D array by 2.
o Add 5 to all elements in the 2D array.
o Perform element-wise addition (+) and multiplication (*) between the 2D array
and itself.
3. Reshape and Transpose:
 Reshape:
3.1. Convert the 1D array into a 2D array with 2 rows and 3 columns.
3.2. Convert the 1D array into a 2D array with 3 rows and 2 columns.
3.3. Flatten the reshaped 2D array in step 3.1 back into a 1D array.
 Transpose the 2D Array:
o Transpose the 2D array created in step 1 to switch rows and columns.

4. Accessing Elements:
o Retrieve the third element and the last element from the 1D array.
o Retrieve the element in the second row and third column of the 2D array.
5. Slicing:
o Extract the first two rows of the 2D array.
o Extract the last two elements of the 1D array.
6. Aggregation functions:
o Calculate the sum of the elements in the 1D array.
o Calculate the sum along columns for the 2D array.
o Calculate the sum along rows for the 2D array.
o Calculate the mean, maximum, and minimum of the 2D array.

Pr. Mehdia AJANA Chapter8 – Python Data Analysis 1


ENSA-Kenitra Module : Compétence Numériques

Exercise 2: Exploring Pandas Basics

Objective: Learn to work with DataFrames and perform basic data operations.

1. Loading Data:
o Create a Pandas DataFrame manually using the following data:

Name Age City Grade


Ali 23 Fes 18.5
Ahmed 21 Rabat 15.25
Morad 22 Tangier 10
Iyad 24 Kenitra 11.75

2. DataFrame Exploration:
o Display the header and the first two rows of the DataFrame.
o Display the header and the last row of the DataFrame.
o Display the column names and data types.
o Find the number of rows and columns in the DataFrame.
3. Data Selection:
o Retrieve the Name column.
o Retrieve rows where the Grade is greater than 12.
4. Adding a Column:
o Add a new column called Passed that indicates whether the student passed
(Grade ≥ 12).
5. Statistics:
o Calculate the average age of the students.
o Find the highest and lowest grade.
6. Saving the Data:
o Drop the column Grade and save the updated DataFrame to a CSV file named
students_updated.csv.

Pr. Mehdia AJANA Chapter8 – Python Data Analysis 2

You might also like