CLASS 12 DATAFRAME WORKSHEET BASED ON DATAFRAME ATTRIBUTES WORKSHEET 4

Section A: Multiple Choice Questions (MCQs)

  1. Which attribute is used to get the labels of the rows and columns in a DataFrame?

    • a) shape
    • b) index
    • c) columns
    • d) axes
  2. The dtype attribute of a DataFrame is used to:

    • a) Find the number of dimensions
    • b) Get the data type of each column
    • c) Check for missing values
    • d) Get the shape of the DataFrame
  3. What does the shape attribute return?

    • a) Number of rows
    • b) Number of columns
    • c) A tuple representing the number of rows and columns
    • d) The size of the DataFrame in memory
  4. The ndim attribute of a DataFrame returns:

    • a) The number of rows
    • b) The number of columns
    • c) The number of dimensions
    • d) The total number of elements
  5. Which attribute would you use to get the total number of elements in a DataFrame?

    • a) size
    • b) shape
    • c) ndim
    • d) axes

Section B: Short Answer Questions

  1. Index and Columns: Given the following DataFrame:


    import pandas as pd
    data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
    df = pd.DataFrame(data)

    Write the Python code to get the index labels and column names of this DataFrame.

  2. Shape and Size: Explain the difference between the shape and size attributes in a DataFrame. Provide a code example to demonstrate their usage.

  3. Data Types: How can you find out the data type of each column in a DataFrame? Illustrate with an example.

  4. Dimensionality: What does the ndim attribute tell you about a DataFrame? Provide an example using a DataFrame with more than two dimensions (e.g., a Panel).

Section C: Practical Task

  1. Create a DataFrame with columns 'ProductID', 'ProductName', and 'Price' with at least 5 rows of data. Write the Python code to:

    • Get the shape of the DataFrame.
    • Retrieve the index and columns of the DataFrame.
    • Determine the size of the DataFrame.
  2. Given a DataFrame with mixed data types (e.g., integers, floats, and strings), write a Python function check_dtype(df) that prints the data type of each column.

  3. Create a DataFrame and demonstrate the use of the axes attribute. Explain how this attribute can be useful when manipulating data.

  4. Consider a DataFrame df with a large number of columns. Write a Python snippet to list all column names along with their data types,

Post a Comment

0 Comments