Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.

Unit 2 MCQs Pandas for Data Handling

CSE273 60 questions. Pick an option to check it.

Read the notes

0 of 60 answered 0 right 0 wrong , 60 left

  1. Question 1 of 60 Series and Data Frame Easy

    In the Pandas library, what is a Series?

  2. Question 2 of 60 Series and Data Frame Easy

    Which of the following best describes a Pandas DataFrame?

  3. Question 3 of 60 Series and Data Frame Easy

    How do you create a simple Pandas DataFrame from a Python dictionary?

  4. Question 4 of 60 Reading and writing CSV file Easy

    Which Pandas function is used to read data from a CSV file into a DataFrame?

  5. Question 5 of 60 Reading and writing CSV file Easy

    To save a DataFrame named df to a CSV file named data.csv, which command should you use?

  6. Question 6 of 60 Inspecting Datasets Easy

    Which method is used to display the first 5 rows of a DataFrame df?

  7. Question 7 of 60 Inspecting Datasets Easy

    What does the .shape attribute of a DataFrame return?

  8. Question 8 of 60 Inspecting Datasets Easy

    Which method provides a concise summary of a DataFrame, including the index dtype, column dtypes, non-null values, and memory usage?

  9. Question 9 of 60 Selecting and filtering data Easy

    How do you select a single column named 'Name' from a DataFrame df?

  10. Question 10 of 60 Selecting and filtering data Easy

    Which expression correctly filters a DataFrame df to show only the rows where the 'Age' column is greater than 25?

  11. Question 11 of 60 Selecting and filtering data Easy

    What is the primary use of the .iloc[] indexer in a Pandas DataFrame?

  12. Question 12 of 60 Data Visualization using Matplotlib Easy

    Which library is most commonly used with Pandas for data visualization and is typically imported as plt?

  13. Question 13 of 60 Line plot Easy

    A line plot is best suited for which of the following tasks?

  14. Question 14 of 60 Bar plot Easy

    Which type of plot is most appropriate for comparing the populations of different countries?

  15. Question 15 of 60 Histogram Easy

    What is the primary purpose of a histogram?

  16. Question 16 of 60 Scatter plot Easy

    A scatter plot is used to visualize the relationship between...

  17. Question 17 of 60 Pie chart Easy

    A pie chart is most effective for representing which of the following?

  18. Question 18 of 60 Customizing plots Easy

    In Matplotlib, which function is used to add a label to the x-axis of a plot?

  19. Question 19 of 60 Customizing plots Easy

    To add a title to a Matplotlib plot, which function would you use?

  20. Question 20 of 60 Subplots Easy

    Which Matplotlib function is commonly used to create a figure and a grid of subplots in a single call?

  21. Question 21 of 60 Series and Data Frame Medium

    You have two Pandas Series, s1 and s2, with different indices. What is the result of the operation s1 + s2?

    Python
    import pandas as pd
    s1 = pd.Series([10, 20], index=['a', 'b'])
    s2 = pd.Series([100, 200], index=['b', 'c'])
    result = s1 + s2
    
    What will `result` look like?

  22. Question 22 of 60 Selecting and filtering data Medium

    Which of the following code snippets correctly selects all rows from a DataFrame df where the 'age' is greater than 30 AND the 'city' is 'New York'?

  23. Question 23 of 60 Inspecting Datasets Medium

    You run df.info() and df.describe() on your DataFrame. What is the key difference in the information they provide?

  24. Question 24 of 60 Reading and writing CSV file Medium

    You are reading a CSV file that uses a semicolon (;) as a delimiter instead of a comma. The file also contains a data row in the first line, meaning it has no header. Which pd.read_csv() call is most appropriate?

  25. Question 25 of 60 Subplots Medium

    Given the setup fig, ax = plt.subplots(2, 2, figsize=(8, 8)), how would you access the Axes object for the plot in the top-right corner to set its title?

  26. Question 26 of 60 Histogram Medium

    What is the primary purpose of a histogram, and what does the bins parameter control?

  27. Question 27 of 60 Selecting and filtering data Medium

    Consider a DataFrame df. If you execute result = df.loc[df['Score'] > 90, 'Name'], what will be the data type of the result object, assuming multiple students have scores over 90?

  28. Question 28 of 60 Series and Data Frame Medium

    What is the output of the following code snippet?

    Python
    import pandas as pd
    data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
    df = pd.DataFrame(data)
    df['C'] = df['A'] * 2
    df.iloc[0, 0] = 100
    print(df.loc[0, 'C'])

  29. Question 29 of 60 Customizing plots Medium

    Which code snippet correctly creates a line plot and adds a title, an x-axis label, and a y-axis label using the object-oriented Matplotlib approach?

  30. Question 30 of 60 Scatter plot Medium

    In the call df.plot(kind='scatter', x='income', y='age', c='num_children', cmap='viridis'), what is the role of the c='num_children' argument?

  31. Question 31 of 60 Reading and writing CSV file Medium

    You have a DataFrame df and you want to save it to a CSV file named 'data.csv' without including the default integer index. Which command should you use?

  32. Question 32 of 60 Selecting and filtering data Medium

    What is the fundamental difference between selecting data with df.loc and df.iloc?

  33. Question 33 of 60 Pie chart Medium

    Under which of the following circumstances is a pie chart generally considered an ineffective or poor choice for data visualization?

  34. Question 34 of 60 Bar plot Medium

    You have a DataFrame df with columns 'Region' and 'Revenue'. You want to create a bar plot showing the total revenue for each region. What is the most common and correct sequence of operations in Pandas?

  35. Question 35 of 60 Inspecting Datasets Medium

    You have a column df['product_type'] in your dataset. Which Pandas method is the most direct way to get a list of all the unique product types and their corresponding frequencies?

  36. Question 36 of 60 Line plot Medium

    A line plot is most suitable for which of the following data visualization tasks?

  37. Question 37 of 60 Customizing plots Medium

    How can you create a single plot that displays two different lines (e.g., from columns 'y1' and 'y2' against 'x') with different colors and a legend to identify them?

  38. Question 38 of 60 Subplots Medium

    What is the primary benefit of using sharex=True or sharey=True when creating subplots with plt.subplots()?

  39. Question 39 of 60 Data Visualization using Matplotlib Medium

    In the Matplotlib object hierarchy, what is the relationship between a Figure and an Axes?

  40. Question 40 of 60 Bar plot Medium

    What is the difference between a simple bar plot and a stacked bar plot?

  41. Question 41 of 60 Selecting and filtering data Hard

    Consider the following DataFrame with a MultiIndex:
    python
    import pandas as pd
    import numpy as np
    arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
    ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
    index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
    df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=['A', 'B'])

    You want to select all rows where the first index level is 'bar' or 'foo', and for those rows, only where the second index level is 'two'. Which of the following lines of code achieves this selection correctly and most efficiently?

  42. Question 42 of 60 Series and Data Frame Hard

    You have a DataFrame df and you execute the following commands:
    python
    df_slice = df.loc[df['A'] > 0]
    df_slice['B'] = 999

    Under which of the following conditions will the original DataFrame df be modified, and why?

  43. Question 43 of 60 Reading and writing CSV file Hard

    You are reading a large (10 GB) CSV file with a 'timestamp' column in ISO 8601 format (e.g., '2023-10-27T10:00:00Z') and a 'value' column. You only have 4 GB of RAM. Your goal is to compute the average 'value' for each calendar year. Which pd.read_csv strategy is most memory-efficient and correct for this task?

  44. Question 44 of 60 Subplots Hard

    You want to create a figure with a complex layout: one large plot on the left spanning two rows, and two smaller plots stacked vertically on the right. You use the following code:
    python
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(8, 6))
    gs = fig.add_gridspec(2, 2)
    ax1 = fig.add_subplot(gs[:, 0])
    ax2 = fig.add_subplot(gs[0, 1])
    ax3 = fig.add_subplot(gs[1, 1])

    Which line of code would correctly set the title of the top-right subplot to "Top Right" and the y-axis label of the large left subplot to "Value"?

  45. Question 45 of 60 Histogram Hard

    You are analyzing a skewed dataset data and plot a histogram using plt.hist(data, bins=50). You then decide to apply a log transformation, log_data = np.log(data), and plot its histogram. What is the primary purpose and expected outcome of applying this transformation before plotting the histogram?

  46. Question 46 of 60 Inspecting Datasets Hard

    You are given a DataFrame df with columns 'Category', 'SubCategory', and 'Sales'. You want to create a summary table that shows the total sales for each 'Category' as rows, each 'SubCategory' as columns, and fills any missing combinations with 0. Additionally, you need to include marginal totals (total sales for each category and each subcategory). Which of the following is the most direct and appropriate method?

  47. Question 47 of 60 Customizing plots Hard

    You are creating a line plot and want to add a horizontal dashed red line at the mean value of the data, and also add a text annotation slightly above this line. Given ax as your Matplotlib axes object and mean_val as the calculated mean, which code snippet correctly achieves this?

  48. Question 48 of 60 Selecting and filtering data Hard

    Given a DataFrame df, what is the functional difference between df.loc[df['A'] > 5] and df.query('A > 5') and when might you strongly prefer one over the other?

  49. Question 49 of 60 Line plot Hard

    You have a pandas DataFrame df with a DatetimeIndex. You plot it using df['value'].plot(). You notice that there are gaps in your time series data (e.g., weekends are missing). When plotted, Matplotlib connects the data points across these gaps with a straight line. How can you create a line plot that shows these discontinuities, i.e., does not connect the points where data is missing?

  50. Question 50 of 60 Scatter plot Hard

    You are creating a scatter plot to show the relationship between 'engine_size' and 'price' for a dataset of cars. You want the size of each point to represent the 'horsepower' and the color of each point to represent the 'fuel_type' (a categorical variable). Which code snippet correctly implements this?

  51. Question 51 of 60 Bar plot Hard

    You have a DataFrame df with columns 'Department', 'Gender', and 'Salary'. You want to create a grouped bar plot showing the average salary for each gender within each department. Which of the following approaches is the most direct way to generate the required data structure and plot it?

  52. Question 52 of 60 Pie chart Hard

    You are creating a pie chart from a pandas Series s that contains the market share of different companies. Some companies have a very small market share (<1%). To make the chart more readable, you want to group all these small companies into a single slice called 'Others'. Which sequence of operations correctly prepares the data for such a pie chart?

  53. Question 53 of 60 Series and Data Frame Hard

    What is the primary difference between the pandas methods .apply() and .transform() when used on a GroupBy object, and in which scenario would .transform() be uniquely suitable?

  54. Question 54 of 60 Reading and writing CSV file Hard

    When writing a DataFrame to a CSV file using df.to_csv('output.csv'), you observe that your floating-point numbers are being written with a large number of decimal places. How can you limit the precision of all floating-point numbers to 4 decimal places directly within the to_csv command?

  55. Question 55 of 60 Data Visualization using Matplotlib Hard

    You have plotted a pandas DataFrame and have the Matplotlib ax object. You want to change the format of the x-axis tick labels from numbers (e.g., 2023.0, 2023.25) to strings representing quarters (e.g., '2023-Q1', '2023-Q2'). Which of the following is the most robust way to achieve this using Matplotlib's functionality?

  56. Question 56 of 60 Inspecting Datasets Hard

    Given a DataFrame df with a column 'A' of type object containing mostly integers but also some non-numeric strings ('error', 'missing'). You attempt to convert it to a numeric type using pd.to_numeric(df['A']) but it fails. What pd.to_numeric parameter setting allows you to convert valid numbers to numeric types while forcing all non-convertible values to NaN, without raising an error?

  57. Question 57 of 60 Subplots Hard

    When creating two subplots with shared X and Y axes using fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True), what is the effect on the tick labels of the resulting plots?

  58. Question 58 of 60 Selecting and filtering data Hard

    You have a DataFrame df with a non-unique index. What is the result of df.loc['duplicate_label'] versus df.iloc[0] if the first row has the label 'duplicate_label'?

  59. Question 59 of 60 Series and Data Frame Hard

    What happens when you add two pandas Series with different indexes and what is the role of the fill_value parameter in the .add() method?

  60. Question 60 of 60 Customizing plots Hard

    In Matplotlib, what is the fundamental difference between the 'state-machine' interface (e.g., plt.plot()) and the 'object-oriented' interface (e.g., ax.plot()), and why is the latter generally preferred for complex plots?

That's unit 2

Answer a few questions and your score shows up here.