0% found this document useful (0 votes)
9 views3 pages

Numpy and Pandas Operations Explained

Uploaded by

Mehak Mehta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Numpy and Pandas Operations Explained

Uploaded by

Mehak Mehta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

a) Consider the following example :


a1=[Link]([12, 23, 34, 45])
a2=[Link](np.string_)
print(a2)
What will be the output of the above code? Justify your answer.

b) Consider the following example :


a3=[Link]([12.3, -23.8, 34.5, 45.8])
print([Link])
a4=[Link](np.int32)
print(a4)
What will be the output of the above code? Justify your answer.

c) Consider the following example :


a5=[Link](['12','-23','34', '45'])
print([Link])
a6=[Link](np.int32)
print(a6)
What will be the output of the above code? Justify your answer.
If we have ‘k’ instead of ‘45’, then what will be the output?

2. For array,
a1=[0, 0, 12, -9, -8, [Link], 23, 0, [Link]],
Use Boolean indexing to display all the non-zero values of the array.

3. Write the name of the method(s) that can be used to create an identity matrix in Numpy.
[Link](2)  2x2 Identity matrix

4. Differentiate between rand() and randn() methods for random generation in Numpy.

5. Differentiate between shape and reshape() for an ndarray, with example.

6. Differentiate between dtype and type() in python with example.


7. Differentiate between zeros() and empty() in numpy with example.
8. Write down 5 methods of Numpy library that can be used to create arrays, excluding array()
and arange() methods.
9. Can you change the data type of an array after its creation? If yes, explain the method with
the help of a brief example. If no, state your reason(s).
10. For a 3D array of shapes (2,3,4), write down the shape of the resultant transposed 3D array
after using T attribute.
11. Differentiate between a view and a copy in the Numpy library, with the help of an example.
12. Explain how homogeneous ndarrays of the Numpy library are better than the heterogeneous
list of Python.
13. Create a 6x6 empty array containing zeros and fill the values as instructed (using fancy
indexing only):
a. Fill all even rows with 1 and all odd rows with -1.
b. Print the last 3 columns of the 2D array.
14. Create a 6x6 empty array containing zeros and fill the values as instructed (using fancy
indexing only):
a. Fill all the diagonal elements with 0.
b. Print the last 3 rows of the 2D array.
15. Differentiate amongst T attribute, swapaxes() and transpose() for numpy array with the help
of an illustrative example.
16. Differentiate between Boolean indexing and fancy indexing with example(s).17. Create
two arrays namely, names and details as shown below:
names = [“Mahak”, “Sawrabh”, “Arpan”, “Mahak”, “Arpan”, “Emmanuel”]
details = {Create a random floating point 2D array of shape (6x4)}
Herein, every row of details array represents the personal details of the corresponding person
in the names array. Use this correlation and perform the following operations using Boolean
indexing:
a. Display all rows related to the person named as “Arpan”.
b. Display all rows related to the person named as “Arpan” and “Sawrabh”.
c. Display all rows except the ones related to “Mahak” or “Emmanuel”.
18. Create a random integer 1D array of 8 elements and split it into 4 equal-sized partitions.
Use these partitions to create two 2x2 arrays namely a1 and a2. Perform floor division between
a1 and a2 and store the result in a3. Scale up the values of a2 by a power of 2 and store the
result in a4. Find out the correlation between arrays a3 and a4.
19. For the following 2D array, write down the appropriate commands that will perform the
required slicing.
(Required slice has been highlighted)

20. Consider the following three lists, wherein each list represents the marks scored by 5
students in their respective GE, SEC and VAC internals (out of 50). Arrange this data into a
dataframe such that every row represents the scores of a student.
GE = [23,49,34,nan,45]
SEC = [nan,34,45,nan,43]
VAC = [30,32,39,nan,25]
Use appropriate functions of pandas library to display following statistical data about the
students:
a. Display the highest score in every subject.
b. Replace the index object of this dataframe with the names of the students. Use the
given list of names=[“Nikunj”, “Rohit”, “Khushi”,”Jordan”, “Himalaya”].
c. Add two new rows to the dataframes with student names as “Harshita” and
“Anshika”. Randomly generate their marks between (0-50).

You might also like