0% found this document useful (0 votes)
15 views7 pages

A.reshape, Resize

The document consists of a series of questions and tasks related to NumPy and Pandas, covering topics such as array manipulation, data filtering, aggregation functions, and DataFrame operations. It includes multiple-choice questions, programming exercises, and theoretical explanations aimed at assessing understanding of these libraries in Python. Additionally, it explores advanced concepts like hierarchical indexing and pivot tables.
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)
15 views7 pages

A.reshape, Resize

The document consists of a series of questions and tasks related to NumPy and Pandas, covering topics such as array manipulation, data filtering, aggregation functions, and DataFrame operations. It includes multiple-choice questions, programming exercises, and theoretical explanations aimed at assessing understanding of these libraries in Python. Additionally, it explores advanced concepts like hierarchical indexing and pivot tables.
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
You are on page 1/ 7

Unit – 4

Part – A

1. The ________ function returns its argument with a modified shape, whereas the ________ method
modifies the array itself
A.reshape, resize
B.resize, reshape
C.reshape2, resize
D.all of the mentioned

2. Which of the following creates a 1D NumPy array?


A.array = np.array([1, 2, 3])
B.array = np.array(1, 2, 3)
C.array = np.array([[1, 2], [3, 4]])
D.array = np.array("1, 2, 3")

3. What does the shape attribute of a NumPy array return?


A.The number of elements in the array
B.The data type of the array
C. A tuple representing the dimensions of the array
D.The size of each element in bytes

4. Which of the following functions returns the average of the elements in a NumPy array?
A.np.sum()
B.np.mean()
C.np.median()
D.np.average()

5. What does np.min() do when applied to a NumPy array?


A.Finds the smallest dimension
B.Returns the index of the smallest value
C.Returns the smallest value in the array
D.Rounds off all values to the minimum

6. Which NumPy function would you use to find the middle value of a dataset?
A.np.mean()
B.np.mode()
C.np.sum()
D.np.median()

7. What will be the result of the following code?


import numpy as np
a = np.array([1, 2, 3])
b=a*2
print(b)
A.[1, 2, 3, 1, 2, 3]
B.[2, 4, 6]
C.[1, 4, 9]
D.Error
8. Which operation is performed by a + b if a and b are NumPy arrays of the same shape?
A.Matrix multiplication
B.Element-wise addition
C.Concatenation
D.Broadcasting

9.If a = np.array([[1, 2], [3, 4]]), what is the result of a**2?


A.[[1, 4], [9, 16]]
B.[[2, 4], [6, 8]]
C.[[1, 2], [3, 4]] ** 2
D.[[1, 2, 3, 4]]

10. What does the expression arr > 5 return if arr is a NumPy array?
A.The elements greater than 5
B. A new array with values set to 5 if condition is true
C. A boolean array where each element indicates whether the condition is true
D.An error

11. What is the purpose of boolean masking in NumPy?


A.To sort the array
B.To hide some values visually
C.To filter elements based on a condition
D.To convert strings to booleans

12. Given arr = np.array([10, 15, 20, 25]), what does arr[arr < 20] return?
A.[10, 15]
B.True
C.[20, 25]
D.[False, False, True, True]

13. What is fancy indexing in NumPy?


A.Indexing using boolean values only
B.Indexing with strings
C. Indexing using arrays of integers or boolean values
D.Indexing with slices only

14. Given arr = np.array([10, 20, 30, 40, 50]), what does arr[[1, 3]] return?
A.[10, 30]
B.[20, 40]
C.[30, 50]
D.[1, 3]

15. If arr = np.array([[1, 2], [3, 4], [5, 6]]), what does arr[[0, 2]] return?
A.[[1, 2], [5, 6]]
B.[1, 5]
C.[2, 6]
D.[[3, 4]]

16. What is the main feature of a structured array in NumPy?


A.It can only store strings
B.It allows storing multi-dimensional arrays
C. It can hold different data types in each column (field)
D.It is faster than normal arrays

17. How do you define a structured array with fields name (string), age (int), and marks (float)?
A.np.array(['name', 'age', 'marks'])
B. np.array([(‘Alice’, 21, 88.5)], dtype=[('name', 'U10'), ('age', 'i4'), ('marks', 'f4')])
C.np.array([‘Alice’, 21, 88.5])
D.np.array(['name', int, float])

18. Which of the following is used to read a CSV file into a Pandas DataFrame?
A.pd.read()
B.pd.DataFrame()
C.pd.read_csv()
D.pd.load_csv()

19. What does the df.drop('column_name', axis=1) function do?


A.Drops a row with the name 'column_name'
B.Renames the column
C.Drops the column named 'column_name'
D.Deletes the entire DataFrame

20. How can you select the first row of a DataFrame df?
A.df[0]
B.df.iloc[0]
C.df.loc[0]
D.df.first()

21. How would you select all rows where the column age is greater than 30 in a DataFrame df?
A.df['age'] > 30
B.df.loc[df.age > 30]
C.df.loc[df['age'] > 30]
D.All of the above

22. What does the df.apply() function do in Pandas?


A. It applies a function to each row or column in the DataFrame
B.It applies a condition to filter rows
C.It applies a function only to numeric columns
D.It applies a function to the DataFrame's index

23. How can you add a new column to a DataFrame df that contains the square of the values in an existing
column age?
A.df['age^2'] = df['age'] ** 2
B.df['age^2'] = df.age * 2
C.df['age^2'] = df.apply(df['age'] ** 2)
D.df['age^2'] = df['age'].map(lambda x: x ** 2)

24. Which of the following methods is used to sort a DataFrame df by a column 'age' in ascending order?
A.df.sort('age')
B.df.sort_values('age')
C.df.order('age')
D.df.sort_by('age')
25. What does df.dropna() do in Pandas?
A.Replaces all missing values with zero
B.Drops all columns
C.Drops all rows that contain any missing values
D.Fills missing values with the column mean

26. Which method is used to fill missing values with a specific value, like 0 or the mean?
A.df.fillna()
B.df.replace()
C.df.setna()
D.df.fixna()

27. What is hierarchical indexing in Pandas?


A.Indexing with single labels only
B. Using multiple levels of indexes (multi-index) in rows or columns
C.Sorting index in reverse order
D.Using index with numeric values only

28. Given a multi-index DataFrame, how can you access data at a specific level of the index?
A.Using .loc[] with a tuple of index values
B.Using .iloc[] with the level name
C.Using .columns[]
D.Using .select()

29. Which function is commonly used to concatenate two or more DataFrames vertically or horizontally?
A.pd.merge()
B.pd.join()
C.pd.concat()
D.pd.append_rows()

30. What is the purpose of pd.merge() in Pandas?


A.To filter data from multiple DataFrames
B. To combine DataFrames based on a common key or column
C.To group data by a column
D.To export DataFrames

31. Which of the following is not a common aggregation function used after grouping in Pandas?
A.sum()
B.mean()
C.count()
D.sort()

32. What will df.groupby('department')['salary'].mean() return?


A.The total salary in each department
B.The average salary for each department
C.The number of employees in each department
D.The entire DataFrame sorted by department

33. In a pivot table, what does the values parameter specify?


A.The rows to display
B.The columns to group by
C.The data to aggregate
D.The index of the table

34. What is the default aggregation function used by pd.pivot_table()?


A.sum
B.count
C.mean
D.max

35. Which of the following will raise an error when using pd.pivot() but works fine with pd.pivot_table()?
A.Multiple aggregation functions
B.Duplicate entries in index/column pairs
C.Missing values
D.Data with strings

PART-B
1.Enumerate attributes of a NumPy array? Explain any three with examples

2. Compare and contrast aggregation using Python built-in functions vs NumPy functions. Explain with
examples and justify why NumPy is preferred.

3. Write a Python program using NumPy to perform the following operations on an array: Square each
element Find the square root Multiply each element by 10 Find the exponential of each element

4. Elucidate Boolean masking in NumPy with an example. How is it useful for data filtering?

5. Write a NumPy program using fancy indexing to: a)Extract multiple rows from a 2D array based on
specific row indices b)Modify the extracted rows by adding 10 to each element

6. Write a NumPy program to create a structured array with fields 'Name', 'Age', and 'Grade', and
demonstrate how to access individual fields using field names.

7. Elucidate how to merge two DataFrames in Pandas. Discuss the different types of joins available and
provide an example for each type.

8. List the difference between .loc[] and .iloc[] in Pandas? Provide examples of how to use both for
selecting rows and columns.

9. Write down the aggregation operations in Pandas? Explain how to use groupby() with an aggregation
function to calculate the mean value of a column based on another categorical column.

10. How do you identify missing data in a Pandas DataFrame? Explain with an example how to check for
NaN values using functions like isna() or isnull().

11. Consider that an E commerce organization like Amazon have different region sales as Northsales,
Southsales, Westsales.csv files. They want combine North and west region sales and south and east sales
to find the aggregate sales of this collaborating region help them to do so using python code.

12. Image you have a series of data that represents the amount of precipitation each day for a year in a
given city. Load the daily rainfall statistics for the city of Chennai in 2021. Which is given in a csv file
Chennai rainfall 2021.csv using Pandas generate a histogram for rainy days and find out the days that
have high rainfall

13. Explain how to use groupby() along with size() to count the number of entries in each group. Provide
an example of how to count the occurrences of each category in a DataFrame.

14. How can you use pivot tables to calculate both the sum and the mean of a particular column for
different categories? Provide an example using a dataset of employee salaries by department.

15. How can you use pivot tables to handle missing data and fill in the gaps in your summarized data?
Demonstrate with an example how fill_value can be used in a pivot table.

PART - C
1.Discuss the fundamental concepts of NumPy arrays and their advantages over traditional Python lists
when performing numerical computations.

2.List and describe five common aggregation functions available in NumPy. Provide a small array and
apply each function.Write the difference between np.sum() and Python’s built-in sum() function when
used on a NumPy array? Use a code example to illustrate the performance difference.

3.import numpy as np
a = np.array([2, 4, 6, 8])
b = np.array([1, 3, 5, 7])
a. Perform and explain the result of the following operations: i. a + b ii. a * b iii. a / b (6 marks)
b. Use NumPy to compute the square and square root of all elements in array a. Show the code and
output. (3 marks)
c. What happens if the shapes of two arrays are not compatible for element-wise operations? Demonstrate
with an example. (3 marks)

4. Briefly explain about Comparision,Masks and Boolean Logic in numpy arrays with a suitable example
program.

5. Elucidate fancy indexing in NumPy? How is it different from regular slicing?Use fancy indexing to
extract the elements at positions 0, 2, and 4 from arr. Write the code and output.

6. import numpy as np
data = np.array([('Alice', 21, 88.5),('Bob', 23, 75.0),('Cathy', 20, 91.2)],dtype=[('name', 'U10'), ('age', 'i4'),
('score', 'f4')])
a. Write code to print the names and scores of all students in the array. (4 marks)
b. Update the score of 'Bob' to 85.0. Show how to do this using indexing. (4 marks)
c. What are the advantages of using structured arrays for datasets like this? (4 marks)

7. Explain the fundamentals of data manipulation with Pandas, including data indexing and selection
techniques using loc and iloc methods. Discuss the advantages of using Pandas DataFrame objects over
NumPy arrays for handling labeled data and performing relational operations.

8. Give an extensive description of data indexing and selection using an appropriate example.

9. Discuss the concept of hierarchical indexing in Pandas and its applications in representing and
analyzing multi-dimensional datasets. Provide examples of hierarchical indexing structures and
demonstrate how they can be used to perform aggregation, grouping on complex datasets.
10. Discuss the concept of hierarchical indexing in Pandas and its applications in representing and
analyzing multi-dimensional datasets. Provide examples of hierarchical indexing structures and
demonstrate how they can be used to perform aggregation, grouping on complex datasets.

11.Elucidate what is pivot table and illustrate how we use the pivot table in data manipulation with
pandas.

You might also like