ASSIGNMENT
Functions to read- random values generation, diagonal values, linspace() for spacing, eye() for
matrix creation
NUMPY
1. Create a NumPy array containing integers from 1 to 10.
ills
2. Create a 2D NumPy array with shape (3, 4) filled with random float values between 0
and 1.
Sk
3. Given the following NumPy array:
arr = [Link]([10, 20, 30, 40, 50])
Add 5 to each element of the array.
4. Given two NumPy arrays:
a
at
arr1 = [Link]([1, 2, 3, 4]) arr2 = [Link]([5, 6, 7, 8])
Concatenate them into a single array.
D
5. Create a NumPy array of 10 elements with equally spaced values from 0 to 9.
w
6. Given a NumPy array:
arr = [Link]([1, 2, 3, 4, 5, 6])
ro
Reverse the elements in the array.
G
7. Create a 3x3 identity matrix using NumPy.
8. Calculate the mean, median, and standard deviation of the following NumPy array:
arr = [Link]([15, 20, 25, 30, 35])
9. Given a 2D NumPy array:
arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Get the diagonal elements of the array.
10. Create a NumPy array of 10 random integers between 1 and 100 (inclusive).
11. Create a 2D NumPy array of shape (5, 5) with random integers between 1 and 50
(inclusive).
ills
12. Given a 2D NumPy array:
arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Sk
Calculate the sum of all the elements in the array.
13. Create a NumPy array with 100 evenly spaced values between 0 and 1 (inclusive) and
reshape it into a 10x10 matrix.
a
14. Given two NumPy arrays:
at
import numpy as np
arr1 = [Link]([1, 2, 3, 4]) arr2 = [Link]([5, 6, 7, 8])
D
Find the common elements between the two arrays.
w
15. Create a NumPy array containing 10 random integers between -50 and 50 (inclusive).
Replace all negative values in the array with 0.
ro
16. Given a 2D NumPy array:
import numpy as np
G
arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Calculate the sum of each row and each column separately.
17. Create a NumPy array of shape (6, 6) with diagonal elements as 1, 2, 3, 4, 5, and 6, and
all other elements as 0.
18. Given a NumPy array:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
Normalize the array so that the values range from 0 to 1.
19. Create a NumPy array with 20 random integers between 1 and 100 (inclusive). Find the
maximum value and its index in the array.
ills
20. Given two NumPy arrays:
import numpy as np
Sk
arr1 = [Link]([1, 2, 3, 4]) arr2 = [Link]([5, 6, 7, 8])
Compute the element-wise product of the two arrays.
a PANDAS
at
Q.1 Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
Input: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]
Output:
D
Add two Series:
0 3
w
1 7
2 11
ro
3 15
4 19
G
dtype: int64
Subtract two Series:
0 1
1 1
2 1
3 1
4 1
dtype: int64
Multiply two Series:
0 2
1 12
2 30
3 56
4 90
ills
dtype: int64
Divide Series1 by Series2:
0 2.000000
Sk
1 1.333333
2 1.200000
3 1.142857
4 1.111111
dtype: float64
a
at
Q.2 Create a Pandas DataFrame from the following dictionary, where the keys represent
columns and the values represent the data:
D
data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 22, 28], 'City': ['New York',
'London', 'Paris', 'Tokyo'] }
w
Q.3 Given a Pandas DataFrame 'df', select the first 5 rows of the DataFrame.
ro
Q.4 Create a new column 'Salary' in the DataFrame 'df' with random integer values
between 50000 and 80000 (inclusive).
G
Q.5 Given two Pandas DataFrames 'df1' and 'df2' with the same columns, concatenate
them vertically.
Q.6 Create a new DataFrame named 'df_filtered' from 'df', containing only rows where
the 'Age' column is greater than 25.
Q.7 Given a Pandas DataFrame 'df', sort the DataFrame based on the 'Age' column in
ascending order.
Q.8 Calculate the mean and median of the 'Salary' column in the DataFrame 'df'.
Q.9 Group the DataFrame 'df' by the 'City' column and calculate the mean 'Age' for each
group.
ills
Q.10 Read the csv provide([Link]) into a Dataframe-
Display:
Sk
*Number of rows and columns in dataframe
* Top 10 and bottom 10 rows
* Summary statistics for numerical columns
a
*Datatype of columns in dataframe
* Length of the data
at
* Print all the Rows from index 100 to 200
D
w
ro
G