You are on page 1of 5

int-215-ca-2

April 15, 2023

1. You have an array of shape (5, 5). Using NumPy, create a new array that contains the
diagonal elements of the original array.
[23]: import numpy as np

# create a 5x5 array


arr = np.array([[1,2,3,4,5], [6,7,8,9,10] ,[11,12,13,14,15] ,[16,17,18,19,20],␣
↪[21,22,23,24,25]])

diagonal_arr = np.diagonal(arr)

# print the original array and the diagonal array


print("Original array:")
print(arr)
print("\nDiagonal array:")
print(diagonal_arr)

Original array:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]

Diagonal array:
[ 1 7 13 19 25]
2. You have two arrays of shape (3, 3) and (3, 1). Using NumPy, perform matrix multiplication
of these arrays.
[2]: import numpy as np

# create the arrays


array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array2 = np.array([[1], [2], [3]])

# matrix multiplication
result = np.dot(array1, array2)

1
print(result)

[[14]
[32]
[50]]
3. You are given an array of integers. Using NumPy, create a new array that contains only the
unique elements of the original array.
[16]: import numpy as np

# create the original array


A = np.array([1, 2, 3, 2, 4, 3, 5, 6, 1])

# find the unique elements


B = np.unique(A)

print(B)

[1 2 3 4 5 6]
4. You have two arrays of shape (3, 3) and (3, 4). Using NumPy, concatenate these arrays along
the first axis.
[17]: import numpy as np

# create the arrays


array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array2 = np.array([[10, 11, 12, 13], [14, 15, 16, 17], [18, 19, 20, 21]])

# concatenate the arrays along the first axis


D = np.concatenate((array1, array2), axis=1)

print(D)

[[ 1 2 3 10 11 12 13]
[ 4 5 6 14 15 16 17]
[ 7 8 9 18 19 20 21]]
6. You have an array of shape (4, 4). Using NumPy, split it into two equal parts horizontally.

[8]: import numpy as np

# create a 4x4 array


arr = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])

2
# split the array horizontally into two parts
arr1, arr2 = np.split(arr, 2, axis=1)

# print the two resulting arrays


print(arr1)
print(arr2)

[[ 1 2]
[ 5 6]
[ 9 10]
[13 14]]
[[ 3 4]
[ 7 8]
[11 12]
[15 16]]
10. You have a data frame containing the names, ages and salaries of employees. Using Pandas,
create a new data frame that contains only the names and salaries of employees who are older
than 30 years.
[1]: import pandas as pd

# create the original data frame


df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 35, 42, 28],
'Salary': [50000, 70000, 90000, 60000]})

# create a new data frame with only names and salaries of employees who are␣
↪older than 30 years

new_df = df.loc[df['Age'] > 30, ['Name', 'Salary']]

print(new_df)

Name Salary
1 Bob 70000
2 Charlie 90000
11. You have two data frames containing the names, ages and genders of students from two
different classes. Using Pandas, merge these data frames on the basis of the names of the
students and add a new column that contains the average age of the students from both
classes.
[14]: import pandas as pd

# create the first data frame


class1 = pd.DataFrame({'Name': ['SITHA', 'RAM', 'ABHI', 'HARI','HARIKA'],
'Age': [25, 35, 42, 28,45],
'Gender': ['Female', 'Male', 'Male', 'Male','Female']})

3
# create the second data frame
class2 = pd.DataFrame({'Name': ['HARIKA', 'RAM', 'SITHA', 'ABHI'],
'Age': [30, 27, 38, 33],
'Gender': ['Female', 'Male', 'Female', 'Male']})

merged = pd.merge(class1, class2, on='Name', how='outer')

#the average age of the students from both classes


merged['Average Age'] = (merged['Age_x'] + merged['Age_y']) / 2

# drop the redundant age columns


merged.drop(['Age_x', 'Age_y'], axis=1, inplace=True)

# print the merged data frame with the new column


print(merged)

Name Gender_x Gender_y Average Age


0 SITHA Female Female 31.5
1 RAM Male Male 31.0
2 ABHI Male Male 37.5
3 HARI Male NaN NaN
4 HARIKA Female Female 37.5
12. You have a data frame containing the names and grades of students. Using Pandas, group the
data frame by grades and calculate the mean, median and standard deviation of the grades
for each group.
[15]: import pandas as pd

# create a sample dataframe


df = pd.DataFrame({
# 'Name': ['RAM', 'RAMU', 'MAHI', 'SURESH', 'ABHI', 'RAHUL' ,␣
↪'RAJ','RAJESH','VIVEK'],

'Marks': [82, 84, 61,63,71, 92,91, 74, 75]


})

# define the grading system


grading_system = {
'A': (90, 100),
'B': (80, 89),
'C': (70, 79),
'D': (60, 69),
'F': (0, 59)
}

# add a new column with grades based on marks


def get_grade(marks):
for grade, (min_marks, max_marks) in grading_system.items():

4
if min_marks <= marks <= max_marks:
return grade

df['Grade'] = df['Marks'].apply(get_grade)

# group by grades and calculate mean, median and standard deviation


grouped_df = df.groupby('Grade').agg(['mean', 'median', 'std'])

print(grouped_df)

Marks
mean median std
Grade
A 91.500000 91.5 0.707107
B 83.000000 83.0 1.414214
C 73.333333 74.0 2.081666
D 62.000000 62.0 1.414214

[ ]:

You might also like